Skip to content
Snippets Groups Projects
Commit 2f5c2b9b authored by Zhenyi Liu's avatar Zhenyi Liu Committed by GitHub
Browse files

Add files via upload

Added Alicloud functions
parent 1854a8bb
No related branches found
No related tags found
No related merge requests found
%% 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
This diff is collapsed.
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
%% 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
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
%% 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
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment