def test_delete_hosts(self, delete_instances_mock): """Test deleting hosts.""" ipa_client_mock = mock.Mock() ipa_client_mock.get_dns_record.side_effect = ipaclient.NotFoundError hostmanager.delete_hosts(mock.Mock(), ipa_client_mock, [ 'test-partition-dq2opb2qrfj.foo.com', 'test-partition-dq2opbqskkq.foo.com', 'test-partition-dq2opc7ao37.foo.com', ]) delete_instances_mock.assert_has_calls([ mock.call(ec2_conn=mock.ANY, hostnames=[ 'test-partition-dq2opb2qrfj.foo.com', 'test-partition-dq2opbqskkq.foo.com', ]), mock.call(ec2_conn=mock.ANY, hostnames=[ 'test-partition-dq2opc7ao37.foo.com', ]), ]) ipa_client_mock.unenroll_host.assert_has_calls([ mock.call('test-partition-dq2opb2qrfj.foo.com'), mock.call('test-partition-dq2opbqskkq.foo.com'), mock.call('test-partition-dq2opc7ao37.foo.com'), ])
def delete(hostname): """Delete instance.""" ipa_client = awscontext.GLOBAL.ipaclient ec2_conn = awscontext.GLOBAL.ec2 hostmanager.delete_hosts(ipa_client=ipa_client, ec2_conn=ec2_conn, hostnames=[hostname])
def delete_hosts(hostnames): """Delete host.""" ipa_client = awscontext.GLOBAL.ipaclient ec2_conn = awscontext.GLOBAL.ec2 hostmanager.delete_hosts(ipa_client=ipa_client, ec2_conn=ec2_conn, hostnames=hostnames)
def _delete_hosts(hostnames): ec2_conn = awscontext.GLOBAL.ec2 ipa_client = awscontext.GLOBAL.ipaclient admin_srv = context.GLOBAL.admin.server() hostmanager.delete_hosts(ipa_client=ipa_client, ec2_conn=ec2_conn, hostnames=hostnames) for hostname in hostnames: admin_srv.delete(hostname)
def delete(hostname, ipa_delete): """Delete instance.""" if ipa_delete: ipa_client = awscontext.GLOBAL.ipaclient else: ipa_client = None ec2_conn = awscontext.GLOBAL.ec2 hostmanager.delete_hosts(ipa_client=ipa_client, ec2_conn=ec2_conn, hostnames=[hostname], ipa_delete=ipa_delete)
def delete_servers_by_name(servers): """Delete servers by name.""" ipa_client = awscontext.GLOBAL.ipaclient ec2_conn = awscontext.GLOBAL.ec2 _LOGGER.info('Deleting servers: %r', servers) hostmanager.delete_hosts(ipa_client=ipa_client, ec2_conn=ec2_conn, hostnames=servers) admin_srv = admin.Server(context.GLOBAL.ldap.conn) for server in servers: admin_srv.delete(server)
def delete_n_servers(count, partition=None): """Delete old servers.""" ipa_client = awscontext.GLOBAL.ipaclient ec2_conn = awscontext.GLOBAL.ec2 admin_srv = admin.Server(context.GLOBAL.ldap.conn) servers = admin_srv.list({ 'cell': context.GLOBAL.cell, 'partition': partition }) hostnames = sorted([s['_id'] for s in servers]) extra = hostnames[0:count] hostmanager.delete_hosts(ipa_client=ipa_client, ec2_conn=ec2_conn, hostnames=extra) for hostname in extra: admin_srv.delete(hostname)
def delete_servers_by_name(servers): """Delete servers by name.""" ec2_conn = awscontext.GLOBAL.ec2 ipa_client = awscontext.GLOBAL.ipaclient admin_srv = context.GLOBAL.admin.server() zkclient = context.GLOBAL.zk.conn _LOGGER.info('Deleting servers: %r', servers) for server in servers: try: presence.kill_node(zkclient, server) except kazoo.exceptions.NoNodeError: pass hostmanager.delete_hosts(ipa_client=ipa_client, ec2_conn=ec2_conn, hostnames=servers) for server in servers: admin_srv.delete(server)
def rotate_cmd(cell, hostname, instance_profile, instance_type, subnet, image, disk): """Rotate cell ZooKeeper server.""" ec2_conn = awscontext.GLOBAL.ec2 ipa_client = awscontext.GLOBAL.ipaclient admin_cell = admin.Cell(context.GLOBAL.ldap.conn) masters = admin_cell.get(cell, dirty=True)['masters'] try: master = next( master for master in masters if master['hostname'] == hostname ) except StopIteration: cli.bad_exit('%s not found in the cell config', hostname) try: ec2_instance = ec2client.get_instance( ec2_conn, hostnames=[hostname] ) _LOGGER.debug(ec2_instance) except exc.NotFoundError: cli.bad_exit('%s EC2 instance does not exist', hostname) hostmanager.delete_hosts(ec2_conn, ipa_client, [hostname]) cli.out('Deleted: %s', hostname) # Copy subnet, type and image from the old instance unless we override. hostmanager.create_zk( ec2_conn=ec2_conn, ipa_client=ipa_client, master=master, subnet_id=subnet or ec2_instance['SubnetId'], instance_type=instance_type or ec2_instance['InstanceType'], instance_profile=instance_profile, image_id=image or ec2_instance['ImageId'], disk=disk ) cli.out('Created: %s', hostname)
def delete(sir_id): """Delete spot instance request""" ipa_client = awscontext.GLOBAL.ipaclient ec2_conn = awscontext.GLOBAL.ec2 for item in ec2client.list_spot_requests(ec2_conn): if item.id == sir_id: sir = item break else: print("SIR %s was not found" % sir_id) return instances = ec2_conn.describe_instances(InstanceIds=[sir.instance_id]) hostname = jmespath.search( "Reservations[].Instances[].[Tags[?Key=='Name'].Value][][]", instances)[0] print("Terminating", hostname) hostmanager.delete_hosts(ipa_client=ipa_client, ec2_conn=ec2_conn, hostnames=[hostname]) print("Cancelling", sir.id) ec2_conn.cancel_spot_instance_requests(SpotInstanceRequestIds=[sir.id])