def setUpClass(cls): """ Initialization of test.""" cls.access_key_id = os.getenv('QY_ACCESS_KEY_ID') cls.secret_access_key = os.getenv('QY_SECRET_ACCESS_KEY') cls.zone = 'pek3' init_conn = APIConnection(qy_access_key_id=cls.access_key_id, qy_secret_access_key=cls.secret_access_key, zone=cls.zone) # Create two test instance. resp = init_conn.run_instances( image_id='xenial4x64a', cpu=1, memory=1024, instance_name='Test_add_InstanceGroupsAction', count=2, login_mode="passwd", login_passwd='Test_add_InstanceGroupsAction99') cls.existed_instances = resp['instances'] cls.group_dict = {'repel_group': None, 'attract_group': None} # Ensure that instances is available for test. while True: status_resp = init_conn.describe_instances( instances=cls.existed_instances) if status_resp['instance_set'][0].get('status') == 'running' and \ status_resp['instance_set'][1].get('status') == 'running': break
def setUp(self): """ Initialization of connection """ # Every action needs the Connection Object for sending request. self.conn = APIConnection(qy_access_key_id=self.access_key_id, qy_secret_access_key=self.secret_access_key, zone=self.zone)
def __init__(self, app_id, secret_app_key, zone, host='api.qingcloud.com', port=443, protocol='https', pool=None, expires=None, retry_time=3, http_socket_timeout=10, access_token=None): """ @param app_id @param secret_app_key @param zone - the zone id to access @param host - the host to make the connection to @param port - the port to use when connect to host @param protocol - the protocol to access to web server, "http" or "https" @param pool - the connection pool @param retry_time - the retry_time when message send fail """ APIConnection.__init__(self, app_id, secret_app_key, zone, host, port, protocol, pool, expires, retry_time, http_socket_timeout) self._auth_handler = auth.AppSignatureAuthHandler( app_id, secret_app_key, access_token)
def tearDownClass(cls): """ Terminate the test instances.""" final_conn = APIConnection(qy_access_key_id=cls.access_key_id, qy_secret_access_key=cls.secret_access_key, zone=cls.zone) try_count = 0 while try_count < cls.max_retry_times + 3: resp = final_conn.terminate_instances( instances=cls.existed_instances, direct_cease=1) if resp['ret_code'] == 1400: try_count += 1 time.sleep(2**try_count) continue elif resp['ret_code'] == 0: final_conn._get_conn(final_conn.host, final_conn.port).close() break
def __init__(self, app_id, secret_app_key, zone, host='api.qingcloud.com', port=443, protocol='https', pool=None, expires=None, retry_time=3, http_socket_timeout=10, access_token=None): """ @param app_id @param secret_app_key @param zone - the zone id to access @param host - the host to make the connection to @param port - the port to use when connect to host @param protocol - the protocol to access to web server, "http" or "https" @param pool - the connection pool @param retry_time - the retry_time when message send fail """ APIConnection.__init__(self, app_id, secret_app_key, zone, host, port, protocol, pool, expires, retry_time, http_socket_timeout) self._auth_handler = auth.AppSignatureAuthHandler(app_id, secret_app_key, access_token)
def main(cls, args): parser = cls.get_argument_parser() options = parser.parse_args(args) directive = cls.build_directive(options) if directive is None: parser.print_help() sys.exit(-1) # load conf conf = load_conf(options.conf_file) if conf is None: sys.exit(-1) conf['expires'] = get_expire_time() if options.zone: conf.update(zone=options.zone) # send request connection = APIConnection(**conf) handler = IaasHandler(connection) return send_request(cls.action, directive, handler)
class TestInstanceGroupsAction(unittest.TestCase): max_retry_times = 2 @classmethod def setUpClass(cls): """ Initialization of test.""" cls.access_key_id = os.getenv('QY_ACCESS_KEY_ID') cls.secret_access_key = os.getenv('QY_SECRET_ACCESS_KEY') cls.zone = 'pek3' init_conn = APIConnection(qy_access_key_id=cls.access_key_id, qy_secret_access_key=cls.secret_access_key, zone=cls.zone) # Create two test instance. resp = init_conn.run_instances( image_id='xenial4x64a', cpu=1, memory=1024, instance_name='Test_add_InstanceGroupsAction', count=2, login_mode="passwd", login_passwd='Test_add_InstanceGroupsAction99') cls.existed_instances = resp['instances'] cls.group_dict = {'repel_group': None, 'attract_group': None} # Ensure that instances is available for test. while True: status_resp = init_conn.describe_instances( instances=cls.existed_instances) if status_resp['instance_set'][0].get('status') == 'running' and \ status_resp['instance_set'][1].get('status') == 'running': break def setUp(self): """ Initialization of connection """ # Every action needs the Connection Object for sending request. self.conn = APIConnection(qy_access_key_id=self.access_key_id, qy_secret_access_key=self.secret_access_key, zone=self.zone) def test01_create_instance_groups(self): # Create a repel-group. resp_repel_group = self.conn.create_instance_groups(relation='repel') self.group_dict.update( repel_group=resp_repel_group['instance_groups'].pop()) self.assertEqual(resp_repel_group['ret_code'], 0) # Create a attract-group. resp_attract_group = self.conn.create_instance_groups( relation='attract') self.group_dict.update( attract_group=resp_attract_group['instance_groups'].pop()) self.assertEqual(resp_attract_group['ret_code'], 0) def test02_join_instance_group(self): existed_instances = [ self.existed_instances[0], self.existed_instances[1] ] tmp = { 'instances': [random.choice(existed_instances)], 'group_id': self.group_dict.get('repel_group') } existed_instances.remove(tmp['instances'][0]) # Add an instance into repel-group. resp_repel = self.conn.join_instance_group( instances=tmp['instances'], instance_group=tmp['group_id']) self.group_dict.update(repel_group=tmp) self.assertEqual(resp_repel['ret_code'], 0) tmp = { 'instances': [random.choice(existed_instances)], 'group_id': self.group_dict.get('attract_group') } # Add an instance into attract-group. resp_attract = self.conn.join_instance_group( instances=tmp['instances'], instance_group=tmp['group_id']) self.group_dict.update(attract_group=tmp) self.assertEqual(resp_attract['ret_code'], 0) def test03_describe_instance_groups(self): repel_id = self.group_dict['repel_group'].get('group_id') resp_repel = self.conn.describe_instance_groups( instance_groups=[repel_id]) self.assertEqual( resp_repel['instance_group_set'][0]['instance_group_id'], repel_id) attract_id = self.group_dict['attract_group'].get('group_id') resp_attract = self.conn.describe_instance_groups( instance_groups=[attract_id]) self.assertEqual( resp_attract['instance_group_set'][0]['instance_group_id'], attract_id) def test04_leave_instance_group(self): try_count = 0 while try_count < self.max_retry_times: try: resp_repel = self.conn.leave_instance_group( instances=self.group_dict['repel_group'].get('instances'), instance_group=self.group_dict['repel_group'].get( 'group_id')) self.assertEqual(resp_repel['ret_code'], 0) except Exception: try_count += 1 time.sleep(2**try_count) pass try_count = 0 while try_count < self.max_retry_times: try: resp_attract = self.conn.leave_instance_group( instances=self.group_dict['attract_group'].get( 'instances'), instance_group=self.group_dict['attract_group'].get( 'group_id')) self.assertEqual(resp_attract['ret_code'], 0) except Exception: try_count += 1 time.sleep(2**try_count) pass def test05_delete_instance_groups(self): try_count = 0 while try_count < self.max_retry_times: check_empty = self.conn.describe_instance_groups(instance_groups=[ self.group_dict['repel_group'].get('group_id'), self.group_dict['attract_group'].get('group_id') ]) if not check_empty['instance_group_set'][0].get('instances') and \ not check_empty['instance_group_set'][1].get('instances'): break resp_del_groups = self.conn.delete_instance_groups(instance_groups=[ self.group_dict['repel_group'].get('group_id'), self.group_dict['attract_group'].get('group_id') ]) self.assertEqual(resp_del_groups['ret_code'], 0) def tearDown(self): """ Close connection. """ self.conn._get_conn(self.conn.host, self.conn.port).close() @classmethod def tearDownClass(cls): """ Terminate the test instances.""" final_conn = APIConnection(qy_access_key_id=cls.access_key_id, qy_secret_access_key=cls.secret_access_key, zone=cls.zone) try_count = 0 while try_count < cls.max_retry_times + 3: resp = final_conn.terminate_instances( instances=cls.existed_instances, direct_cease=1) if resp['ret_code'] == 1400: try_count += 1 time.sleep(2**try_count) continue elif resp['ret_code'] == 0: final_conn._get_conn(final_conn.host, final_conn.port).close() break
def connect_to_zone(zone, access_key_id, secret_access_key, host,port,protocol,lowercase=True): """ Connect to one of zones in qingcloud by access key. """ # if lowercase: # zone = zone.strip().lower() return APIConnection(access_key_id, secret_access_key, zone,host,port,protocol)
def connect_to_zone(zone, access_key_id, secret_access_key, lowercase=True): """ Connect to one of zones in qingcloud by access key. """ if lowercase: zone = zone.strip().lower() return APIConnection(access_key_id, secret_access_key, zone)
""" 该脚本主要用于已部署环境的节点的制作镜像、将镜像id写入mustache文件,并重启节点 """ class Error(Exception): pass logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', datefmt='%Y/%m/%d %H:%M:%S', level=logging.ERROR) conn = APIConnection(qy_access_key_id=config.access_key_id, qy_secret_access_key=config.secret_access_key, zone=config.zone, host="api.qingcloud.com", port=443, protocol="https") def exec_cmd(command, timeout=20): start = datetime.datetime.now() process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) while process.poll() is None: time.sleep(0.1) now = datetime.datetime.now() if timeout is not None and (now - start).seconds > timeout: os.kill(process.pid, signal.SIGKILL)
def connect_to_zone(zone, access_key_id, secret_access_key): """ Connect to one of zones in qingcloud by access key. """ return APIConnection(access_key_id, secret_access_key, zone)