diff --git a/Alicloud/AlicloudInit.m~ b/Alicloud/AlicloudInit.m~ new file mode 100644 index 0000000000000000000000000000000000000000..d35e0d6cd8b4e9d273f535e6e0c9d6f5695aa17a --- /dev/null +++ b/Alicloud/AlicloudInit.m~ @@ -0,0 +1,40 @@ +%% Kubernete Initialization +% Create a Kubernetes cluster using ROS service provided by Alicloud using a template which is located in current folder. +cmd = sprintf('python /Library/Frameworks/Python.framework/Versions/2.7/bin/ros --json create-stack --stack-name ros-demo --template-url kube_3master.json --parameters MasterInstanceType=ecs.n1.medium,WorkerInstanceType=ecs.n1.medium,ImageId=centos_7,NumOfNodes=1,LoginPassword=Project2017'); +[~, result] = system(cmd); +result; +result = erase(result,'[Succeed]'); +result = parse_json(result); +StackID = result.Id +% check status of creating process +while 1 + cmd = sprintf('python /Library/Frameworks/Python.framework/Versions/2.7/bin/ros --json describe-stack --stack-name ros-demo --stack-id %s',StackID); + [~, result] = system(cmd); + result_check = erase(result,'[Succeed]'); + result_check = parse_json(result_check); + status = result_check.Status +if strcmp(status,'CREATE_COMPLETE')== 1; + break; +end +end +% Check the jump ip +result = parse_json(result); +result = erase(str,'[succeed]'); +JumasterIp = result.Outpus{2}; +% MasterIp = result.Outputs{4}; +% Copy kube.config to local + +% copy kube config file from alicloud master machine to local machine +cmd = sprintf('scp root@%s:/etc/kubernetes/kube.conf $HOME/.kube/config',masterIp) +system(cmd); +% Validate template +% cmd = sprintf('python /Library/Frameworks/Python.framework/Versions/2.7/bin/ros validate-template --template-url kube_3master.json') +% [~, result] = system(cmd); +% result; + +% Delet a kluster +%cmd = sprintf('python /Library/Frameworks/Python.framework/Versions/2.7/bin/ros delete-stack --region-id us-west-1 --stack-name ros-demo --stack-id %s',StackID); +%system(cmd) +% Create a buket + +% Connect a buket to kubernetes \ No newline at end of file diff --git a/Alicloud/kube_3master.json b/Alicloud/kube_3master.json new file mode 100644 index 0000000000000000000000000000000000000000..dc9c09b2c57deeb008ef57cad1d97640bd15fb01 --- /dev/null +++ b/Alicloud/kube_3master.json @@ -0,0 +1,623 @@ +{ + "ROSTemplateFormatVersion": "2015-09-01", + "Description": "Need Activate RAM service", + "Parameters": { + "MasterInstanceType": { + "Type": "String", + "Default": "ecs.n4.large", + "AllowedValues": [ + "ecs.n1.medium", + "ecs.n1.large", + "ecs.n2.medium", + "ecs.n2.large", + "ecs.n4.large", + "ecs.n4.xlarge", + "ecs.mn4.large", + "ecs.mn4.xlarge", + "ecs.e4.large", + "ecs.cm4.xlarge", + "ecs.ce4.xlarge", + "ecs.c4.xlarge", + "ecs.se1.large", + "ecs.se1.xlarge", + "ecs.se1.2xlarge", + "ecs.se1.4xlarge", + "ecs.se1.8xlarge", + "ecs.se1.14xlarge", + "ecs.se1ne.large", + "ecs.se1ne.xlarge", + "ecs.se1ne.2xlarge", + "ecs.se1ne.4xlarge", + "ecs.se1ne.8xlarge", + "ecs.se1ne.14xlarge" + ], + "Label": "ECS instance specification of Master node", + "Description": "Creates ECS instances with the specification for the Master node of Kubernetes" + }, + "WorkerInstanceType": { + "Type": "String", + "Default": "ecs.n4.large", + "AllowedValues": [ + "ecs.n1.medium", + "ecs.n1.large", + "ecs.n2.medium", + "ecs.n2.large", + "ecs.n4.large", + "ecs.n4.xlarge", + "ecs.mn4.large", + "ecs.mn4.xlarge", + "ecs.e4.large", + "ecs.cm4.xlarge", + "ecs.ce4.xlarge", + "ecs.c4.xlarge", + "ecs.se1.large", + "ecs.se1.xlarge", + "ecs.se1.2xlarge", + "ecs.se1.4xlarge", + "ecs.se1.8xlarge", + "ecs.se1.14xlarge", + "ecs.se1ne.large", + "ecs.se1ne.xlarge", + "ecs.se1ne.2xlarge", + "ecs.se1ne.4xlarge", + "ecs.se1ne.8xlarge", + "ecs.se1ne.14xlarge" + ], + "Label": "ECS instance specification of Worker node", + "Description": "Create ESC instances with the specification for the Worker node of Kubernetes" + }, + "ImageId": { + "Type": "String", + "Default": "centos_7", + "Label": "System image of ECS instance", + "Description": "System image for initializing ECS" + }, + "NumOfNodes": { + "Type": "Number", + "Default": "2", + "Label": "The number of worker node", + "Description": "Specifies the number of Worker nodes to create Kubernetes" + }, + "LoginPassword": { + "Type": "String", + "NoEcho": true, + "MaxLength": 41, + "MinLength": 8, + "AllowedPattern": "[a-zA-Z0-9-\\(\\)\\`\\~\\!@\\#\\$%\\^&\\*-+=\\|\\{\\}\\[\\]\\:\\;\\?\\,\\.\\?\\/]*", + "Description": "ECS login password", + "Label": "ECS login password" + } + }, + "Resources": { + "k8s_vpc": { + "Type": "ALIYUN::ECS::VPC", + "Properties": { + "CidrBlock": "192.168.0.0/16", + "VpcName": "k8s_vpc" + } + }, + "k8s_vswitch": { + "Type": "ALIYUN::ECS::VSwitch", + "Properties": { + "CidrBlock": "192.168.0.0/16", + "ZoneId": { + "Fn::Select": ["0", { + "Fn::GetAZs": { + "Ref": "ALIYUN::Region" + } + }] + }, + "VpcId": { + "Ref": "k8s_vpc" + } + } + }, + "k8s_NAT_Gateway_SNAT": { + "Properties": { + "AllocatePublicIP": true, + "InternetChargeType": "PayByTraffic", + "InternetMaxBandwidthIn": 100, + "InternetMaxBandwidthOut": 5, + "IoOptimized": "optimized", + "InstanceType":{ + "Ref": "WorkerInstanceType" + }, + "ImageId": {"Ref": "ImageId"}, + "SystemDiskCategory": "cloud_ssd", + "SecurityGroupId":{ + "Ref": "k8s_sg" + }, + "VpcId": { + "Ref":"k8s_vpc" + }, + "VSwitchId": { + "Ref": "k8s_vswitch" + }, + "Password":{ + "Ref":"LoginPassword" + }, + "UserData": { + "Fn::Join": [ + "", + [ + "#!/bin/sh", + "\n", + "PostRouting=192.168.0.0/16\n", + "SourceRouting=`ifconfig eth0|grep inet|awk '{print $2}'|tr -d 'addr:'`", + "\n", + "echo 'net.ipv4.ip_forward=1'>> /etc/sysctl.conf \n", + "sysctl -p \n", + "iptables -t nat -I POSTROUTING -s $PostRouting -j SNAT --to-source $SourceRouting \n", + "iptables-save\n" + ] + ] + } + }, + "Type": "ALIYUN::ECS::Instance" + }, + "k8s_route_entry": { + "Type": "ALIYUN::ECS::Route", + "Properties": { + "RouteId": {"Fn::GetAtt": ["k8s_vpc", "VRouterId" ]}, + "RouteTableId": {"Fn::GetAtt": ["k8s_vpc", "RouteTableId" ]}, + "DestinationCidrBlock": "0.0.0.0/0", + "NextHopId": { + "Fn::GetAtt": [ + "k8s_NAT_Gateway_SNAT", + "InstanceId" + ] + } + } + }, + "k8s_sg": { + "Type": "ALIYUN::ECS::SecurityGroup", + "Properties": { + "SecurityGroupName": "k8s_sg", + "VpcId": { + "Ref": "k8s_vpc" + }, + "SecurityGroupIngress": [ + { + "SourceCidrIp": "0.0.0.0/0", + "IpProtocol": "all", + "NicType": "intranet", + "PortRange": "-1/-1", + "Priority": 1 + } + ], + "SecurityGroupEgress": [ + { + "DestCidrIp": "0.0.0.0/0", + "IpProtocol": "all", + "NicType": "intranet", + "PortRange": "-1/-1", + "Priority": 1 + } + ], + } + }, + "k8s_master_slb": { + "Type": "ALIYUN::SLB::LoadBalancer", + "Properties": { + "VpcId": { + "Ref": "k8s_vpc" + }, + "VSwitchId": { + "Ref": "k8s_vswitch" + }, + "AddressType": "intranet" + } + }, + "k8s_master_slb_listener": { + "Type": "ALIYUN::SLB::Listener", + "Properties": { + "LoadBalancerId": {"Ref": "k8s_master_slb"}, + "ListenerPort": 6443, + "BackendServerPort": 6443, + "Protocol": "tcp", + "Bandwidth": 100 + } + }, + "k8s_master_public_slb": { + "Type": "ALIYUN::SLB::LoadBalancer", + "Properties": { + "AddressType": "internet" + } + }, + "k8s_master_public_slb_listener": { + "Type": "ALIYUN::SLB::Listener", + "Properties": { + "LoadBalancerId": {"Ref": "k8s_master_public_slb"}, + "ListenerPort": 6443, + "BackendServerPort": 6443, + "Protocol": "tcp", + "Bandwidth": 100 + } + }, + "k8s_master1_cloudinit_wait_cond_handle": { + "Type": "ALIYUN::ROS::WaitConditionHandle" + }, + "k8s_master1_cloudinit_wait_cond": { + "Type": "ALIYUN::ROS::WaitCondition", + "Properties": { + "Handle": { + "Ref": "k8s_master1_cloudinit_wait_cond_handle" + }, + "Timeout": 600, + "Count": 1 + } + }, + "k8s_master_1": { + "Type": "ALIYUN::ECS::Instance", + "DependsOn": ["k8s_route_entry","k8s_master_public_slb_listener", "k8s_master_slb_listener"], + "Properties": { + "ImageId": {"Ref": "ImageId"}, + "InstanceType": {"Ref": "MasterInstanceType"}, + "SecurityGroupId": { + "Ref": "k8s_sg" + }, + "VpcId": { + "Ref": "k8s_vpc" + }, + "VSwitchId": { + "Ref": "k8s_vswitch" + }, + "Password": {"Ref": "LoginPassword"}, + "PrivateIpAddress": "192.168.0.1", + "AllocatePublicIP": true, + "IoOptimized": "optimized", + "SystemDiskCategory": "cloud_ssd", + "UserData": { + "Fn::Replace": [{ + "ros-notify": { + "Fn::GetAtt": ["k8s_master1_cloudinit_wait_cond_handle", "CurlCli"] + } + }, { + "Fn::Join": ["", [ + "#!/bin/sh\n", + "ssh-keygen -t rsa -P '' -f '/root/.ssh/id_rsa' \n", + "cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys\n", + "echo 'StrictHostKeyChecking no' > /root/.ssh/config\n", + "ros-notify\n" + ]] + }] + } + } + }, + "k8s_master2_cloudinit_wait_cond_handle": { + "Type": "ALIYUN::ROS::WaitConditionHandle" + }, + "k8s_master2_cloudinit_wait_cond": { + "Type": "ALIYUN::ROS::WaitCondition", + "DependsOn": ["k8s_master1_cloudinit_wait_cond"], + "Properties": { + "Handle": { + "Ref": "k8s_master2_cloudinit_wait_cond_handle" + }, + "Timeout": 600, + "Count": 1 + } + }, + "k8s_master_2": { + "Type": "ALIYUN::ECS::Instance", + "DependsOn": ["k8s_master1_cloudinit_wait_cond"], + "Properties": { + "ImageId": {"Ref": "ImageId"}, + "InstanceType": {"Ref": "MasterInstanceType"}, + "SecurityGroupId": { + "Ref": "k8s_sg" + }, + "VpcId": { + "Ref": "k8s_vpc" + }, + "VSwitchId": { + "Ref": "k8s_vswitch" + }, + "Password": {"Ref": "LoginPassword"}, + "PrivateIpAddress": "192.168.0.2", + "AllocatePublicIP": false, + "IoOptimized": "optimized", + "SystemDiskCategory": "cloud_ssd", + "UserData": { + "Fn::Replace": [{ + "ros-notify": { + "Fn::GetAtt": ["k8s_master2_cloudinit_wait_cond_handle", "CurlCli"] + } + }, { + "Fn::Join": ["", [ + "#!/bin/sh\n", + "ssh-keygen -t rsa -P '' -f '/root/.ssh/id_rsa' \n", + "until yum -y install expect ; do echo 'wait yum ready ...'; sleep 1; done\n", + "touch /root/scp_key.sh\n", + "echo '#!/usr/bin/expect' >> /root/scp_key.sh\n", + "echo 'spawn scp root@192.168.0.1:/root/.ssh/* /root/.ssh/' >> /root/scp_key.sh\n", + "echo 'set timeout 30' >> /root/scp_key.sh\n", + "echo 'expect \"*yes*\"' >> /root/scp_key.sh\n", + "echo 'send \"yes\\r\"' >> /root/scp_key.sh\n", + "echo 'expect \"*password:\"' >> /root/scp_key.sh\n", + "echo 'send \"", + {"Ref": "LoginPassword"}, + "\\r\"' >> /root/scp_key.sh\n", + "echo 'expect EOF' >> /root/scp_key.sh\n", + "expect /root/scp_key.sh\n", + "rm -rf /root/scp_key.sh\n", + "ros-notify\n" + ]] + }] + } + } + }, + "k8s_master_cloudinit_wait_cond_handle": { + "Type": "ALIYUN::ROS::WaitConditionHandle" + }, + "k8s_master_cloudinit_wait_cond": { + "Type": "ALIYUN::ROS::WaitCondition", + "DependsOn": ["k8s_master1_cloudinit_wait_cond","k8s_master2_cloudinit_wait_cond"], + "Properties": { + "Handle": { + "Ref": "k8s_master_cloudinit_wait_cond_handle" + }, + "Timeout": 2400, + "Count": 1 + } + }, + "k8s_master_3": { + "Type": "ALIYUN::ECS::Instance", + "DependsOn": ["k8s_master1_cloudinit_wait_cond","k8s_master2_cloudinit_wait_cond"], + "Properties": { + "ImageId": {"Ref": "ImageId"}, + "InstanceType": {"Ref": "MasterInstanceType"}, + "SecurityGroupId": { + "Ref": "k8s_sg" + }, + "VpcId": { + "Ref": "k8s_vpc" + }, + "VSwitchId": { + "Ref": "k8s_vswitch" + }, + "Password": {"Ref": "LoginPassword"}, + "PrivateIpAddress": "192.168.0.3", + "AllocatePublicIP": false, + "IoOptimized": "optimized", + "SystemDiskCategory": "cloud_ssd", + "UserData": { + "Fn::Replace": [{ + "ros-notify": { + "Fn::GetAtt": ["k8s_master_cloudinit_wait_cond_handle", "CurlCli"] + } + }, { + "Fn::Join": ["", [ + "#!/bin/sh\n", + "ssh-keygen -t rsa -P '' -f '/root/.ssh/id_rsa' \n", + "until yum -y install expect ; do echo 'wait yum ready ...'; sleep 1; done\n", + "touch /root/scp_key.sh\n", + "echo '#!/usr/bin/expect' >> /root/scp_key.sh\n", + "echo 'spawn scp root@192.168.0.1:/root/.ssh/* /root/.ssh/' >> /root/scp_key.sh\n", + "echo 'set timeout 30' >> /root/scp_key.sh\n", + "echo 'expect \"*yes*\"' >> /root/scp_key.sh\n", + "echo 'send \"yes\\r\"' >> /root/scp_key.sh\n", + "echo 'expect \"*password:\"' >> /root/scp_key.sh\n", + "echo 'send \"", + {"Ref": "LoginPassword"}, + "\\r\"' >> /root/scp_key.sh\n", + "echo 'expect EOF' >> /root/scp_key.sh\n", + "expect /root/scp_key.sh\n", + "rm -rf /root/scp_key.sh\n", + "curl http://aliacs-k8s.oss-cn-hangzhou.aliyuncs.com/installer/kubemgr-1.7.2-ha.sh >admin.sh ;chmod +x admin.sh\n", + "./admin.sh --node-type etcd --host1 192.168.0.1 --host2 192.168.0.2 --host3 192.168.0.3 --oss-region cn-hongkong\n", + "./admin.sh --node-type master --oss-region cn-hongkong --key-id ", + {"Fn::GetAtt": ["k8s_ram_user_ak", "AccessKeyId"]}, + " --key-secret ", + {"Fn::GetAtt": ["k8s_ram_user_ak", "AccessKeySecret"]}, + " --host1 192.168.0.1 --host2 192.168.0.2 --host3 192.168.0.3 --api-server-slb ", + {"Fn::GetAtt": ["k8s_master_slb", "IpAddress"]}, + " --extra-sans ", + {"Fn::GetAtt": ["k8s_master_public_slb", "IpAddress"]}, + "\n", + "TOKEN=`kubeadm token list | grep token |awk '{print $1}'`\n", + "./admin.sh --node-type master_dispatch --oss-region cn-hongkong ", + " --host1 192.168.0.1 --host2 192.168.0.2 --host3 192.168.0.3 --token $TOKEN --key-id ", + {"Fn::GetAtt": ["k8s_ram_user_ak", "AccessKeyId"]}, + " --key-secret ", + {"Fn::GetAtt": ["k8s_ram_user_ak", "AccessKeySecret"]}, + " --api-server-slb ", + {"Fn::GetAtt": ["k8s_master_slb", "IpAddress"]}, + " --extra-sans ", + {"Fn::GetAtt": ["k8s_master_public_slb", "IpAddress"]}, + "\n", + "echo 'Sleep 20s to wait Master be ready.'; sleep 20\n", + "echo 'Send notify to ROS notify server, TOKEN='$TOKEN\n", + "ros-notify -d \"{\\\"data\\\": \\\"$TOKEN\\\"}\" \n" + ]] + }] + } + } + }, + "k8s_master_slb_attachements": { + "Type": "ALIYUN::SLB::BackendServerAttachment", + "Properties": { + "LoadBalancerId": {"Ref": "k8s_master_slb"}, + "BackendServerList": [ + {"Ref": "k8s_master_1"}, + {"Ref": "k8s_master_2"}, + {"Ref": "k8s_master_3"} + ] + } + }, + "k8s_master_public_slb_attachements": { + "Type": "ALIYUN::SLB::BackendServerAttachment", + "Properties": { + "LoadBalancerId": {"Ref": "k8s_master_public_slb"}, + "BackendServerList": [ + {"Ref": "k8s_master_1"}, + {"Ref": "k8s_master_2"}, + {"Ref": "k8s_master_3"} + ] + } + }, + "k8s_node_cloudinit_wait_cond_handle": { + "Type": "ALIYUN::ROS::WaitConditionHandle" + }, + "k8s_node_cloudinit_wait_cond": { + "Type": "ALIYUN::ROS::WaitCondition", + "DependsOn": ["k8s_master_cloudinit_wait_cond","k8s_master_slb_attachements"], + "Properties": { + "Handle": { + "Ref": "k8s_node_cloudinit_wait_cond_handle" + }, + "Timeout": 2400, + "Count": {"Ref": "NumOfNodes"} + } + }, + "k8s_nodes": { + "Type" : "ALIYUN::ECS::InstanceGroup", + "DependsOn": ["k8s_master_cloudinit_wait_cond","k8s_master_slb_attachements"], + "Properties" : { + "ImageId": {"Ref": "ImageId"}, + "InstanceType": {"Ref": "WorkerInstanceType"}, + "SecurityGroupId": { + "Ref": "k8s_sg" + }, + "VpcId": { + "Ref": "k8s_vpc" + }, + "VSwitchId": { + "Ref": "k8s_vswitch" + }, + "Password": {"Ref": "LoginPassword"}, + "AllocatePublicIP": false, + "IoOptimized": "optimized", + "SystemDiskCategory": "cloud_ssd", + "MaxAmount": {"Ref": "NumOfNodes"}, + "MinAmount": {"Ref": "NumOfNodes"}, + "UserData": { + "Fn::Replace": [{ + "ros-notify": { + "Fn::GetAtt": ["k8s_node_cloudinit_wait_cond_handle", "CurlCli"] + } + }, { + "Fn::Join": ["", [ + "#!/bin/sh\n", + "ssh-keygen -t rsa -P '' -f '/root/.ssh/id_rsa' \n", + "until yum -y install expect ; do echo 'wait yum ready ...'; sleep 1; done\n", + "touch /root/scp_key.sh\n", + "echo '#!/usr/bin/expect' >> /root/scp_key.sh\n", + "echo 'spawn scp root@192.168.0.1:/root/.ssh/* /root/.ssh/' >> /root/scp_key.sh\n", + "echo 'set timeout 30' >> /root/scp_key.sh\n", + "echo 'expect \"*yes*\"' >> /root/scp_key.sh\n", + "echo 'send \"yes\\r\"' >> /root/scp_key.sh\n", + "echo 'expect \"*password:\"' >> /root/scp_key.sh\n", + "echo 'send \"", + {"Ref": "LoginPassword"}, + "\\r\"' >> /root/scp_key.sh\n", + "echo 'expect EOF' >> /root/scp_key.sh\n", + "expect /root/scp_key.sh\n", + "rm -rf /root/scp_key.sh\n", + "echo 'Deploy K8S nodes...'\n", + "scp root@192.168.0.3:/etc/kubernetes/cloud-config ./cloud-config\n", + "export ACCESS_KEY_SECRET=`cat ./cloud-config|grep accessKeySecret|awk -F '\"' '{print $4}'`\n", + "rm -rf ./cloud-config\n", + "export TOKEN=`echo '", + {"Fn::GetAtt": ["k8s_master_cloudinit_wait_cond", "Data"]}, + "' | awk -F '\"' '{print $4}'`\n", + "echo 'TOKEN='$TOKEN\n", + "curl http://aliacs-k8s.oss-cn-hangzhou.aliyuncs.com/installer/kubemgr-1.7.2-ha.sh >admin.sh ;chmod +x admin.sh\n", + "./admin.sh --node-type node --oss-region cn-hongkong --key-id ", + {"Fn::GetAtt": ["k8s_ram_user_ak", "AccessKeyId"]}, + " --key-secret $ACCESS_KEY_SECRET", + " --token $TOKEN --endpoint ", + {"Fn::GetAtt": ["k8s_master_slb", "IpAddress"]}, + ":6443\n", + "echo 'Send notify to ROS notify server.'\n", + "ros-notify\n" + ]] + }] + } + } + }, + "k8s_ram_user": { + "Type": "ALIYUN::RAM::User", + "Properties": { + "UserName": { + "Fn::Join": [ + "", [ + "k8s_ram_user_", { + "Ref": "ALIYUN::StackId" + } + ] + ] + } + } + }, + "k8s_ram_user_policy": { + "Type": "ALIYUN::RAM::ManagedPolicy", + "Properties": { + "PolicyName": { + "Fn::Join": [ + "", [ + "k8sRAMUserPolicy", { + "Ref": "ALIYUN::StackId" + } + ] + ] + }, + "PolicyDocument": { + "Version": "1", + "Statement": [{ + "Action": [ + "*" + ], + "Resource": [ + "*" + ], + "Effect": "Allow" + }] + }, + "Users": [{ + "Fn::GetAtt": [ + "k8s_ram_user", + "UserName" + ] + }] + } + }, + "k8s_ram_user_ak": { + "Type": "ALIYUN::RAM::AccessKey", + "Properties": { + "UserName": { + "Fn::GetAtt": [ + "k8s_ram_user", + "UserName" + ] + } + } + } + }, + "Outputs": { + "APIServer_Intranet": { + "Value": { + "Fn::Join": ["", [ + {"Fn::GetAtt": ["k8s_master_slb", "IpAddress"]}, + ":6443" + ] + ] + }, + "Description": "kubernetes API Server private network IP" + }, + "APIServer_Internet": { + "Value": { + "Fn::Join": ["", [ + {"Fn::GetAtt": ["k8s_master_public_slb", "IpAddress"]}, + ":6443" + ] + ] + }, + "Description": "kubernetes API Server public network IP" + }, + "masterIp": { + "Value": {"Fn::GetAtt": ["k8s_master_1", "PublicIp"]}, + "Description": "master IP" + } + } +} \ No newline at end of file diff --git a/Alicloud/parse_json.m b/Alicloud/parse_json.m new file mode 100644 index 0000000000000000000000000000000000000000..f4a6e7da8898b8651e64cdd05d6ffa2bedb32038 --- /dev/null +++ b/Alicloud/parse_json.m @@ -0,0 +1,200 @@ +function data = parse_json(string) +% DATA = PARSE_JSON(string) +% This function parses a JSON string and returns a cell array with the +% parsed data. JSON objects are converted to structures and JSON arrays are +% converted to cell arrays. + +pos = 1; +len = length(string); +% String delimiters and escape characters are identified beforehand to improve speed +esc = regexp(string, '["\\]'); index_esc = 1; len_esc = length(esc); + +if pos <= len + switch(next_char) + case '{' + data = parse_object; + case '[' + data = parse_array; + otherwise + error_pos('Outer level structure must be an object or an array'); + end +end + + function object = parse_object + parse_char('{'); + object = []; + if next_char ~= '}' + while 1 + str = parse_string; + if isempty(str) + error_pos('Name of value at position %d cannot be empty'); + end + parse_char(':'); + val = parse_value; + object.(valid_field(str)) = val; + if next_char == '}' + break; + end + parse_char(','); + end + end + parse_char('}'); + end + + function object = parse_array + parse_char('['); + object = cell(0, 1); + if next_char ~= ']' + while 1 + val = parse_value; + object{end+1} = val; + if next_char == ']' + break; + end + parse_char(','); + end + end + parse_char(']'); + end + + function parse_char(c) + skip_whitespace; + if pos > len || string(pos) ~= c + error_pos(sprintf('Expected %c at position %%d', c)); + else + pos = pos + 1; + skip_whitespace; + end + end + + function c = next_char + skip_whitespace; + if pos > len + c = []; + else + c = string(pos); + end + end + + function skip_whitespace + while pos <= len && isspace(string(pos)) + pos = pos + 1; + end + end + + function str = parse_string + if string(pos) ~= '"' + error_pos('String starting with " expected at position %d'); + else + pos = pos + 1; + end + str = ''; + while pos <= len + while index_esc <= len_esc && esc(index_esc) < pos + index_esc = index_esc + 1; + end + if index_esc > len_esc + str = [str string(pos:end)]; + pos = len + 1; + break; + else + str = [str string(pos:esc(index_esc)-1)]; + pos = esc(index_esc); + end + switch string(pos) + case '"' + pos = pos + 1; + return; + case '\' + if pos+1 > len + error_pos('End of file reached right after escape character'); + end + pos = pos + 1; + switch string(pos) + case {'"' '\' '/'} + str(end+1) = string(pos); + pos = pos + 1; + case {'b' 'f' 'n' 'r' 't'} + str(end+1) = sprintf(['\' string(pos)]); + pos = pos + 1; + case 'u' + if pos+4 > len + error_pos('End of file reached in escaped unicode character'); + end + str(end+1:end+6) = string(pos-1:pos+4); + pos = pos + 5; + end + otherwise % should never happen + str(end+1) = string(pos); + pos = pos + 1; + end + end + error_pos('End of file while expecting end of string'); + end + + function num = parse_number + [num, one, err, delta] = sscanf(string(pos:min(len,pos+20)), '%f', 1); % TODO : compare with json(pos:end) + if ~isempty(err) + error_pos('Error reading number at position %d'); + end + pos = pos + delta-1; + end + + function val = parse_value + switch(string(pos)) + case '"' + val = parse_string; + return; + case '[' + val = parse_array; + return; + case '{' + val = parse_object; + return; + case {'-','0','1','2','3','4','5','6','7','8','9'} + val = parse_number; + return; + case 't' + if pos+3 <= len && strcmpi(string(pos:pos+3), 'true') + val = true; + pos = pos + 4; + return; + end + case 'f' + if pos+4 <= len && strcmpi(string(pos:pos+4), 'false') + val = false; + pos = pos + 5; + return; + end + case 'n' + if pos+3 <= len && strcmpi(string(pos:pos+3), 'null') + val = []; + pos = pos + 4; + return; + end + end + error_pos('Value expected at position %d'); + end + + function error_pos(msg) + poss = max(min([pos-15 pos-1 pos pos+20],len),1); + if poss(3) == poss(2) + poss(3:4) = poss(2)+[0 -1]; % display nothing after + end + msg = [sprintf(msg, pos) ' : ... ' string(poss(1):poss(2)) '<error>' string(poss(3):poss(4)) ' ... ']; + ME = MException('JSONparser:invalidFormat', msg); + throw(ME); + end + + function str = valid_field(str) + % From MATLAB doc: field names must begin with a letter, which may be + % followed by any combination of letters, digits, and underscores. + % Invalid characters will be converted to underscores, and the prefix + % "alpha_" will be added if first character is not a letter. + if ~isletter(str(1)) + str = ['alpha_' str]; + end + str(~isletter(str) & ~('0' <= str & str <= '9')) = '_'; + end + +end \ No newline at end of file diff --git a/Alicloud/rtbAlicloudInit.m b/Alicloud/rtbAlicloudInit.m new file mode 100644 index 0000000000000000000000000000000000000000..bcc6ba8b143a74b628aa4d19ece9ad68a91f85a2 --- /dev/null +++ b/Alicloud/rtbAlicloudInit.m @@ -0,0 +1,54 @@ +%% Kubernete Initialization +% Create a Kubernetes cluster using a template. +clear; close all; +cmd = sprintf('python /Library/Frameworks/Python.framework/Versions/2.7/bin/ros --json create-stack --stack-name ros-demo --template-url /Users/eugeneliu/git_repo/RenderToolbox4/Alicloud/kube_3master.json --parameters MasterInstanceType=ecs.n1.medium,WorkerInstanceType=ecs.n1.medium,ImageId=centos_7,NumOfNodes=1,LoginPassword=Project2017'); +[~, result] = system(cmd); +result = erase(result,'[Succeed]'); +result = parse_json(result); +StackID = result.Id; + +% check status of creating process +while 1 + cmd = sprintf('python /Library/Frameworks/Python.framework/Versions/2.7/bin/ros --json describe-stack --stack-name ros-demo --stack-id %s',StackID); + [~, result] = system(cmd); + result_check = erase(result,'[Succeed]'); + result_check = parse_json(result_check); + status = result_check.Status; + pause(30); + fprintf('%s\n',status); +if strcmp(status,'CREATE_COMPLETE')== 1 + break; + +end +end +% Check the master ip +%result = erase(result,'[succeed]'); +%result = parse_json(result); +masterIp = result_check.Outputs{2}.OutputValue; + +% copy kube config file from alicloud master machine to local machine. +cmd = sprintf('scp root@%s:/etc/kubernetes/kube.conf $HOME/.kube/config',masterIp); +system(cmd); +% Validate template +% cmd = sprintf('python /Library/Frameworks/Python.framework/Versions/2.7/bin/ros validate-template --template-url kube_3master.json') +% [~, result] = system(cmd); +% result; + +% Delet a kluster +%cmd = sprintf('python /Library/Frameworks/Python.framework/Versions/2.7/bin/ros delete-stack --region-id us-west-1 --stack-name ros-demo --stack-id %s',StackID); +%system(cmd) +% Create a buket + +% Connect a bucket to kubernetes + +% Create a bucket +% ossutilmac64 is a file that needed to be downloaded. +% bucketname =('zhenyi0929'); +% cmd = sprintf('/Users/eugeneliu/Downloads/ossutilmac64 mb oss://%s', bucketname); %the name can be put into hints. +% system(cmd); +% % Upload local file to cloud bucket. +% cmd = sprintf('/Users/eugeneliu/Downloads/ossutilmac64 cp %s oss://%s', bucketname,localdir) ;%the name can be put into hints. +% system(cmd); +% % Download files from cloud. +% cmd = sprintf('/Users/eugeneliu/Downloads/ossutilmac64 cp oss://%s %s', bucketname,localdir); %the name can be put into hints. +% system(cmd); \ No newline at end of file diff --git a/Alicloud/rtbCloudUpload_Ali.m b/Alicloud/rtbCloudUpload_Ali.m new file mode 100644 index 0000000000000000000000000000000000000000..4667b76295e6c895fb06d9103dfd3d3b77156f3d --- /dev/null +++ b/Alicloud/rtbCloudUpload_Ali.m @@ -0,0 +1,36 @@ +function rtbCloudUpload_Ali( hints, nativeSceneFiles ) + +% Upload all the data from the working directory into the cloud + +% No need to authenticate on the local system +% cmd = sprintf('gcloud auth activate-service-account --key-file=%s',hints.batchRenderStrategy.renderer.tokenPath); +% system(cmd); + +if strcmp(hints.renderer,'PBRTCloud') == 0 + return; +end + +fileName = hints.batchRenderStrategy.renderer.getDataFileName; +allFiles = cell2mat(strcat(nativeSceneFiles,{' '})); + +allFilesAndFolders = sprintf('%s ./resources ./scenes',allFiles); + +currentPath = pwd; +cd(hints.batchRenderStrategy.renderer.workingFolder); +cmd = sprintf('zip -r %s/%s %s -x *.jpg *.png',hints.batchRenderStrategy.renderer.workingFolder,fileName,allFilesAndFolders); +system(cmd); +cd(currentPath); +% Google and Alibaba use different commands for files uploading. +%if strcmp(hints.batchRenderStrategy.renderer.provider,'Alicloud') == 1 %add in the hints + cmd = sprintf('/Users/eugeneliu/Downloads/ossutilmac64 cp %s/%s oss://docker2017',hints.batchRenderStrategy.renderer.workingFolder,fileName); + %cmd = sprintf('/Users/eugeneliu/Downloads/ossutilmac64 cp %s/%s %s',hints.batchRenderStrategy.renderer.workingFolder,fileName,... + %hints.batchRenderStrategy.renderer.cloudFolder);% Ali uses oss bucket as a clould storage + system(cmd);% Ali use an unix executable file instead of a simple command +%else +% cmd = sprintf('gsutil cp %s/%s %s/',hints.batchRenderStrategy.renderer.workingFolder,fileName,... +% hints.batchRenderStrategy.renderer.cloudFolder); +%system(cmd); +%end + +end + diff --git a/Alicloud/v_acloudTest.m b/Alicloud/v_acloudTest.m new file mode 100644 index 0000000000000000000000000000000000000000..96ee1dcb9f4e975d619c3e18fe7d853a07020ed0 --- /dev/null +++ b/Alicloud/v_acloudTest.m @@ -0,0 +1,30 @@ +%% v_acloudTest +% +% Try the different acloud methods and make sure they are all working +% +% ZL Vistasoft Team 2017 +clear all; +aliyun = acloud; +disp(aliyun) + +%% Create a bucket +bname = 'vistabucket'; % we recommand a unique name for bucketname rather than an ordinary name like 'test' or 'testbucket' +aliyun.bucketCreate(bname); +aliyun.ls % list the buckets + +%% upload a local file to the bucket. +aliyun.upload('RenderToolbox4/testfile_acloud.m',bname) +aliyun.ls(bname)% list the contents in the bucket + +%% Delete the loacl file first, and Downlaod the file from bucket to local. +delete('RenderToolbox4/testfile_acloud.m')% You can manually delete the file as well. +aliyun.download(bname,'RenderToolbox4'); +% You can find the file again in your current folder. + +%% Delete the object in the bucket +aliyun.objectrm('vistabucket/testfile_acloud.m') +aliyun.ls(bname) + +%% Delete the bucket +aliyun.bucketrm(bname) +aliyun.ls \ No newline at end of file diff --git a/acloud/acloud.m b/acloud/acloud.m new file mode 100644 index 0000000000000000000000000000000000000000..b68fd3ef42fdec2df76336471d66673fb7ce0984 --- /dev/null +++ b/acloud/acloud.m @@ -0,0 +1,141 @@ +classdef acloud < handle + % Create an alibaba cloud object to interact with aliyun + % You need to set up your alibaba account (see <https://account.aliyun.com/register/register.htm>) + % + % For testing see v_acloudTest.m + % + % ZL Vistasoft Team 2017 + properties + bucket = 'oss://'; + ros = 'python /Library/Frameworks/Python.framework/Versions/2.7/bin/ros --json'; + end + methods + function obj = acloud(varargin) +% p = inputParser; +% p.addParameter('bucket','oss://',@ischar); +% p.addParameter('ros','python /Library/Frameworks/Python.framework/Versions/2.7/bin/ros --json',@ischar) +% p.parse(varargin{:}); +% obj.bucket = p.Results.bucket; +% obj.ros = p.Results.ros; + end + function [result, status, cmd] = ls(obj,bucketname) + if ieNotDefined('bucketname') + d = obj.bucket; + else + d = fullfile(obj.bucket, bucketname); + end + cmd = sprintf('/Applications/ossutil ls %s\n',d); + [status,result] = system(cmd); + end + function [result, status, cmd] = objectrm(obj,objectname) + if ieNotDefined('objectname') + disp('Object name required') + else + objname = fullfile(obj.bucket,objectname); + cmd = sprintf('/Applications/ossutil rm %s \n',objname); + [status, result] = system(cmd); + end + end + function [result, status, cmd] = bucketrm(obj,bucketname) + if ieNotDefined('bucketname') + disp('Bucket name required') + else + bname = fullfile(obj.bucket,bucketname); + cmd = sprintf('/Applications/ossutil rm %s -b -f\n',bname); + [status, result] = system(cmd); + end + end + function [result, status, cmd] = bucketCreate(obj,bucketname) + if ieNotDefined('bucketname') + disp('Bucket name (lower case) required') + else + bucketname = lower(bucketname); + bname = fullfile(obj.bucket,bucketname); + cmd = sprintf('/Applications/ossutil mb %s \n',bname); + [status, result] = system(cmd); + end + end + function [result, status, cmd] = upload(obj,local_dir,cloud_dir) + cloud_dir = fullfile(obj.bucket,cloud_dir); + cmd = sprintf('/Applications/ossutil cp %s %s -r -f -u\n',local_dir,cloud_dir); + [status, result] = system(cmd); + end + function [result, status, cmd] = download(obj,cloud_dir,local_dir) + cloud_dir = fullfile(obj.bucket,cloud_dir); + cmd = sprintf('/Applications/ossutil cp %s %s -r -f -u\n',cloud_dir,local_dir); + [status, result] = system(cmd); + end + function [result, status, masterIp, cmd] = k8sCreate(obj,stackname, MasterInstanceType,WorkerInstanceType,NumberOfNodes) + if ieNotDefined('MasterInstanceType') + MasterType = ecs.n1.medium; + else + MasterType = MasterInstanceType; + end + if ieNotDefined('WorkerInstanceType') + WorkerType = ecs.n1.medium; + else + WorkerType = WorkerInstanceType; + end + if ieNotDefined('NumberOfNodes') + NumNodes = 2; + else + NumNodes = NumberOfNodes; + end + cmd = sprintf('%s create-stack --stack-name %s --template-url /Users/eugeneliu/git_repo/RenderToolbox4/Alicloud/kube_3master.json --parameters MasterInstanceType=%s,...WorkerInstanceType=%s,ImageId=centos_7,NumOfNodes=%s,LoginPassword=Project2017',... + obj.ros,stackname,MasterType,WorkerType,NumNodes); + [~, result] = system(cmd); + result = erase(result,'[Succeed]'); + result = parse_json(result); + StackID = result.Id; + while 1 + cmd = sprintf('python /Library/Frameworks/Python.framework/Versions/2.7/bin/ros --json describe-stack --stack-name ros-demo --stack-id %s',StackID); + [~, result] = system(cmd); + result_check = erase(result,'[Succeed]'); + result_check = parse_json(result_check); + status = result_check.Status; + pause(60); + fprintf('%s\n',status); + if strcmp(status,'CREATE_COMPLETE')== 1 + break; + end + masterIp = result_check.Outputs{2}.OutputValue; + end + + + end + end +end + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +