Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jar包&&镜像更新到0.9.1 #435

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 34 additions & 32 deletions appbuilder/utils/bce_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def __init__(self, config_path):
self.config_path = config_path
self.load_config()

self.credentials = BceCredentials(
self.bce_config["ak"], self.bce_config["sk"])
self.credentials = BceCredentials(self.bce_config["ak"], self.bce_config["sk"])
self.bos_client = self.create_bos_client()
self.bcc_client = self.create_bce_client()

Expand All @@ -55,12 +54,13 @@ def load_config(self):

def save_config(self, config):
self.config["bce_config"]["security_group_id"] = self.security_group_id
with open(self.config_path, 'w') as f:
with open(self.config_path, "w") as f:
yaml.dump(config, f, indent=4)

def create_bce_client(self):
bce_config = BceClientConfiguration(
self.credentials, endpoint=self.bce_config["host"])
self.credentials, endpoint=self.bce_config["host"]
)
return InnerBccClient(bce_config)

def create_bos_client(self):
Expand All @@ -86,53 +86,55 @@ def bos_upload(self):
self.bos_client.create_bucket(bucket_name)

self.bos_client.put_object_from_file(
bucket_name, self.tar_file_name, self.tar_file_name)
bucket_name, self.tar_file_name, self.tar_file_name
)
timestamp = int(time.time())
url = self.bos_client.generate_pre_signed_url(
bucket_name, self.tar_file_name, timestamp, expiration_in_seconds=3600
)
self.tar_bos_url = url.decode("utf-8")
if self.tar_bos_url == None:
raise Exception("upload to bos failed")
self.log.debug(
"upload to bos successfully! url: {}".format(self.tar_bos_url))
self.log.debug("upload to bos successfully! url: {}".format(self.tar_bos_url))

def clear_local(self):
os.remove(self.run_script)
os.remove(self.tar_file_name)

def build_user_data(self):
workspace = self.appbuilder_config["workspace"]
user_data = "#!/bin/bash\\n" + \
"mkdir /root/test\\n" + \
"chmod 777 /root/test\\n" + \
"cd /root/test\\n" + \
f"wget -O {self.tar_file_name} {self.tar_bos_url}\\n" + \
f"tar -xvf {self.tar_file_name}\\n" + \
f"rm {self.tar_file_name}\\n" + \
f"chmod a+x {self.run_script_name}\\n" + \
"yum install -y docker\\n" + \
"docker pull registry.baidubce.com/appbuilder/appbuilder-sdk-cloud:0.9.0\\n" + \
f"docker run -itd --net=host -v /root/test:{workspace} --name appbuilder-sdk registry.baidubce.com/appbuilder/appbuilder-sdk-cloud:0.9.0 {workspace}/{self.run_script_name}"
user_data = (
"#!/bin/bash\\n"
+ "mkdir /root/test\\n"
+ "chmod 777 /root/test\\n"
+ "cd /root/test\\n"
+ f"wget -O {self.tar_file_name} {self.tar_bos_url}\\n"
+ f"tar -xvf {self.tar_file_name}\\n"
+ f"rm {self.tar_file_name}\\n"
+ f"chmod a+x {self.run_script_name}\\n"
+ "yum install -y docker\\n"
+ "docker pull registry.baidubce.com/appbuilder/appbuilder-sdk-cloud:0.9.1\\n"
+ f"docker run -itd --net=host -v /root/test:{workspace} --name appbuilder-sdk registry.baidubce.com/appbuilder/appbuilder-sdk-cloud:0.9.1 {workspace}/{self.run_script_name}"
)

return user_data

def build_run_script(self):
timestamp = int(time.time())
self.run_script_name = "start_" + str(timestamp) + ".sh"
self.run_script = os.path.join(
self.appbuilder_config["local_dir"], self.run_script_name)
self.appbuilder_config["local_dir"], self.run_script_name
)

commands = []
workspace = self.appbuilder_config["workspace"]
for key, value in self.env.items():
commands.append(f'export {key}="{value}"')
run_cmd = " && ".join(commands) + " && " + \
self.appbuilder_config["run_cmd"]
run_cmd = " && ".join(commands) + " && " + self.appbuilder_config["run_cmd"]

with open(self.run_script, 'w') as file:
file.write('#!/bin/sh\n')
file.write(f'cd {workspace}\n')
with open(self.run_script, "w") as file:
file.write("#!/bin/sh\n")
file.write(f"cd {workspace}\n")
file.write(run_cmd)

def create_instance(self):
Expand All @@ -156,8 +158,7 @@ def create_instance(self):
self.get_instance_id(instance)
if self.instance_id == None:
raise Exception("create instance failed")
self.log.info(
"instance create successfully! id: {}".format(self.instance_id))
self.log.info("instance create successfully! id: {}".format(self.instance_id))

def get_instance_id(self, instance):
self.instance_id = instance.instance_ids[0]
Expand Down Expand Up @@ -204,13 +205,15 @@ def create_security_group(self):
if self.security_group_id == None:
raise Exception("create security group failed")

self.log.info("security group create successfully!id: {}".format(
self.security_group_id))
self.log.info(
"security group create successfully!id: {}".format(self.security_group_id)
)
self.save_config(self.config)

def bind_security_group(self):
response = self.bcc_client.bind_instance_to_security_group(
self.instance_id, self.security_group_id)
self.instance_id, self.security_group_id
)
self.log.debug("bind instance to security group: {}".format(response))

def _pre_deploy(self):
Expand All @@ -232,8 +235,7 @@ def _deploy(self):
def _after_deploy(self):
self.get_public_ip()
self.bind_security_group()
self.log.info(
"deployment finished! public ip: {}".format(self.public_ip))
self.log.info("deployment finished! public ip: {}".format(self.public_ip))

def deploy(self):
self._pre_deploy()
Expand All @@ -250,5 +252,5 @@ def deploy():
instance.deploy()


if __name__ == '__main__':
if __name__ == "__main__":
deploy()
Loading
Loading