def update_aliIngress(c): from aliyun_cloud_api import modifySec,describeSec # ret=c.run('curl ipinfo.io/ip').stdout.strip() with open(cache_file,'rb') as input_file: host_ip=input_file.read().decode() taskinfo(host_ip) cred=[os.getenv('ALIYUN_SECRET_ID'), os.getenv('ALIYUN_SECRET_KEY')] resp = describeSec(*cred) taskinfo(host_ip==resp.Permissions.Permission[0].SourceCidrIp) if host_ip is not resp.Permissions.Permission[0].SourceCidrIp: taskinfo('need to update the ip address') modifySec(host_ip,*cred) resp = describeSec(*cred) taskinfo(resp.Permissions.Permission[0].SourceCidrIp) taskinfo(host_ip==resp.Permissions.Permission[0].SourceCidrIp)
def get_aliIngress(c): from aliyun_cloud_api import describeIns,describeSec,modifySec cred=[os.getenv('ALIYUN_SECRET_ID'), os.getenv('ALIYUN_SECRET_KEY')] resp = describeSec(*cred) taskinfo(resp.Permissions.Permission[0].SourceCidrIp)
def des_aliIns_ip(c): from aliyun_cloud_api import describeIns cred=[os.getenv('ALIYUN_SECRET_ID'), os.getenv('ALIYUN_SECRET_KEY')] inst=describeIns(*cred) inst_focus=inst.Instances.Instance[0] Status=inst_focus.InstanceId,inst_focus.Status IpAddress= inst_focus.PublicIpAddress['IpAddress'][0] taskdebug(inst_focus) taskinfo("Inst: {0}:{1}".format(inst_focus.InstanceId,inst_focus.Status)) taskinfo("Inst: {0}:{1}".format(inst_focus.InstanceId,inst_focus.PublicIpAddress['IpAddress'][0]))
def get_tcIngress(c): from tencentcloud.common import credential from tencent_cloud_api import updateIngress,getPolicyset,describeIns cred = credential.Credential( # django_settings.TC_SECRET_ID, # django_settings.TC_SECRET_KEY os.getenv('TENCENTCLOUD_SECRET_ID'), os.getenv('TENCENTCLOUD_SECRET_KEY') ) inst = getPolicyset(cred) # taskinfo("the TotalCount of Inst: {}".format(inst.TotalCount)) taskinfo("The Ingress IP Address of Security Group {}".format(inst.SecurityGroupPolicySet.Ingress[3].CidrBlock)) taskinfo("The Version of Security Group {}".format(inst.SecurityGroupPolicySet.Version))
def des_tcIns(c): from tencentcloud.common import credential from tencent_cloud_api import updateIngress,getPolicyset,describeIns cred = credential.Credential( # django_settings.TC_SECRET_ID, # django_settings.TC_SECRET_KEY os.getenv('TENCENTCLOUD_SECRET_ID'), os.getenv('TENCENTCLOUD_SECRET_KEY') ) # return a instance inst = describeIns(cred) # instance = updataIngress(cred) taskinfo("the TotalCount of Inst: {}".format(inst.TotalCount))
def update_hosts(c): from aliyun_cloud_api import describeIns cred=[os.getenv('ALIYUN_SECRET_ID'), os.getenv('ALIYUN_SECRET_KEY')] inst=describeIns(*cred) inst_focus=inst.Instances.Instance[0] Status=inst_focus.InstanceId,inst_focus.Status IpAddress= inst_focus.PublicIpAddress['IpAddress'][0] with open('/etc/hosts','ab') as io: io.write('{} aliyun_host'.format(IpAddress).encode()) taskdebug(inst_focus) taskinfo("Inst: {0}:{1}".format(inst_focus.InstanceId,inst_focus.Status)) taskinfo("Inst: {0}:{1}".format(inst_focus.InstanceId,inst_focus.PublicIpAddress['IpAddress'][0]))
def stop_aliIns(c): from aliyun_cloud_api import startstopInst,describeIns cred=[os.getenv('ALIYUN_SECRET_ID'), os.getenv('ALIYUN_SECRET_KEY')] with open(cache_inst_file,'rb') as file_input: inst_id=file_input.read().decode() if not inst_id: taskdebug('empty inst_id') return startstopInst(inst_id,False,*cred) inst=describeIns(*cred) inst_focus=inst.Instances.Instance[0] taskinfo("Inst: {0}:{1}".format(inst_focus.InstanceId,inst_focus.Status))
def des_aliIns(c): from aliyun_cloud_api import describeIns cred=[os.getenv('ALIYUN_SECRET_ID'), os.getenv('ALIYUN_SECRET_KEY')] inst=describeIns(*cred) inst_focus=inst.Instances.Instance[0] Status=inst_focus.InstanceId,inst_focus.Status # IpAddress= inst_focus.PublicIpAddress['IpAddress'][0] taskdebug('save the InstanceId') with open(cache_inst_file,'wb') as input_file: input_file.write(inst_focus.InstanceId.encode()) taskdebug(inst_focus) taskinfo("Inst: {0}:{1}".format(inst_focus.InstanceId,inst_focus.Status))