Esempio n. 1
0
def bootVM(image, flavor, keyname, hostname, desc, min_count=1, max_count=1):
    nova = nvclient.Client(username=os.environ['OS_USERNAME'],
      project_id=os.environ['OS_TENANT_NAME'],
      api_key=os.environ['OS_PASSWORD'],
      auth_url=os.environ['OS_AUTH_URL'],
      insecure=True)

    image = nova.images.find(id=image)
    flavor = nova.flavors.find(name=flavor)

    # réseau privé sur Cloud OMC / IMA
    net_id="67a3e2c9-424a-463a-8a73-cccde3de4443"
    nics = [{"net-id": net_id, "v4-fixed-ip": ''}]

    instance = nova.servers.create(name=hostname,
                                   image=image,
                                   flavor=flavor,
                                   meta=desc,
                                   min_count=min_count,
                                   max_count=max_count,
                                   nics=nics,
                                   key_name=keyname)
    return instance
    def test_auth_system_defining_url(self, mock_iter_entry_points):
        """Test the auth_system defining an url."""
        class MockEntrypoint(pkg_resources.EntryPoint):
            def load(self):
                return FakePlugin

        class FakePlugin(auth_plugin.BaseAuthPlugin):
            def get_auth_url(self):
                return "http://faked/v2.0"

        mock_iter_entry_points.side_effect = lambda _t: [
            MockEntrypoint("fake", "fake", ["FakePlugin"])
        ]

        auth_plugin.discover_auth_systems()
        plugin = auth_plugin.load_plugin("fake")

        cs = client.Client("username",
                           "password",
                           "project_id",
                           auth_system="fakewithauthurl",
                           auth_plugin=plugin)
        self.assertEquals(cs.client.auth_url, "http://faked/v2.0")
Esempio n. 3
0
    def createKeypair(self, keypair_name, cloud):
        user, password, tenant, url = self.authenCloud(configfile,cloud)
        nova_client = client.Client(user, password, tenant, url)
        if not os.path.exists("./ssh"):
            os.makedirs("./ssh")
        private_key_filename = "./ssh/id_TUD"
        public_key_filename = "./ssh/id_TUD.pub"
        home = expanduser("~")
        keypair = nova_client.keypairs.create(name=keypair_name)

        # Create a file for writing that can only be read and written by owner
        fprivate = os.open(private_key_filename, os.O_WRONLY | os.O_CREAT, 0o600)
        fpublic = os.open(public_key_filename, os.O_WRONLY | os.O_CREAT, 0o600)
        with os.fdopen(fprivate, 'w') as f:
            f.write(keypair.private_key)
        with os.fdopen(fpublic, 'w') as f:
            f.write(keypair.public_key)

        dst = home + "/.ssh/"
        shutil.copy(private_key_filename, dst)
        shutil.copy(public_key_filename, dst)
        private_key = home + "/.ssh/id_TUD"
        call(["ssh-add", private_key])
Esempio n. 4
0
def delete_handle(message):
    novaDao = DRNovaDao()
    logger = logging.getLogger("NovaHandler")
    url = message['Request']['url'].split('/')
    server_id = url[len(url) - 1]
    cf = ConfigParser.ConfigParser()
    cf.read("/home/eshufan/projects/drcontroller/drcontroller/conf/set.conf")
    try:
        drc_id = novaDao.get_by_primary_instance_uuid(
            server_id).secondary_instance_uuid
    except:
        return
    logger.info('Delete shadow VM ' + drc_id + ' for ' + server_id +
                ' in dr site')
    if (drc_id != None):
        drc_ncred = {}
        drc_ncred['auth_url'] = cf.get("drc", "auth_url")
        drc_ncred['username'] = cf.get("drc", "user")
        drc_ncred['api_key'] = cf.get("drc", "password")
        drc_ncred['project_id'] = cf.get("drc", "tenant_name")
        drc_nova = novaclient.Client(**drc_ncred)
        drc_nova.servers.delete(drc_id)
        novaDao.delete_by_primary_instance_uuid(server_id)
Esempio n. 5
0
    def __init__(self, name, conf, os_auth_info):
        BaseClient.__init__(self, name, conf, os_auth_info)
        self.handle = client.Client(
                           os_auth_info["username"],
                           os_auth_info["password"],
                           os_auth_info["tenant_name"],
                           os_auth_info["auth_url"],
                           insecure=True,
                           service_type="compute")

        # Maybe the client doesn't prefer ssh route
        self.ssh_info = conf.get("ssh", None)

        servers = []
        try:
            servers.extend(self.handle.servers.list())
        except NotFound:
            logging.warn("No servers present for client %s" % name)

        self.pattern = re.compile("^" + os_auth_info["username"] + "-[0-9]{3}")
        for inst in servers:
            if not self.pattern.match(inst.name):
                continue
            instance = Instance(inst, self.ssh_info)
            instanceId = instance.get_id()

            self.id2inst[instanceId] = instance

            vols = []
            try:
                vols.extend(self.handle.volumes.get_server_volumes(instanceId))
            except NotFound:
                logging.warn("No volume attached for instance %s(%s)" % (instance.get_name(), instance.get_id()))

            volumes = self.id2vols[instanceId] = []
            for vol in vols:
                volumes.append(Volume(instance, vol))
Esempio n. 6
0
def novaclient(context):

    compat_catalog = {
        # TODO(gbasava): Check this...   'access': {'serviceCatalog': context.service_catalog or []}
        'access': []
    }
    sc = service_catalog.ServiceCatalog(compat_catalog)
    if CONF.nova_endpoint_template:
        url = CONF.nova_endpoint_template % context.to_dict()
    else:
        info = CONF.nova_catalog_info
        service_type, service_name, endpoint_type = info.split(':')
        # extract the region if set in configuration
        if CONF.os_region_name:
            attr = 'region'
            filter_value = CONF.os_region_name
        else:
            attr = None
            filter_value = None
        url = sc.url_for(attr=attr,
                         filter_value=filter_value,
                         service_type=service_type,
                         service_name=service_name,
                         endpoint_type=endpoint_type)

    LOG.debug(_('Novaclient connection created using URL: %s') % url)

    c = nova_client.Client(context.user_id,
                           context.auth_token,
                           project_id=context.project_id,
                           auth_url=url,
                           insecure=CONF.nova_api_insecure)
    # noauth extracts user_id:tenant_id from auth_token
    c.client.auth_token = context.auth_token or '%s:%s' % (context.user_id,
                                                           context.project_id)
    c.client.management_url = url
    return c
Esempio n. 7
0
def main():

    argument_spec = openstack_argument_spec()
    argument_spec.update(dict(
            ip_address                      = dict(required=True),
            instance_name                   = dict(required=True),
            state                           = dict(default='present', choices=['absent', 'present'])
    ))
    module = AnsibleModule(argument_spec=argument_spec)

    if not HAVE_DEPS:
        module.fail_json(msg='python-novaclient, python-keystoneclient, and either python-neutronclient or python-quantumclient are required')

    try:
        nova = nova_client.Client(module.params['login_username'], module.params['login_password'],
                                 module.params['login_tenant_name'], module.params['auth_url'], service_type='compute')
    except Exception as e:
        module.fail_json( msg = " Error in authenticating to nova: %s" % e.message)
    neutron = _get_neutron_client(module, module.params)
    state, floating_ip_id = _get_floating_ip_id(module, neutron)
    if module.params['state'] == 'present':
        if state == 'attached':
            module.exit_json(changed = False, result = 'attached', public_ip=module.params['ip_address'])
        server_info, server_obj = _get_server_state(module, nova)
        if not server_info:
            module.fail_json(msg = " The instance name provided cannot be found")
        port_id = _get_port_id(neutron, module, server_info['id'])
        if not port_id:
            module.fail_json(msg = "Cannot find a port for this instance, maybe fixed ip is not assigned")
        _update_floating_ip(neutron, module, port_id, floating_ip_id)

    if module.params['state'] == 'absent':
        if state == 'detached':
            module.exit_json(changed = False, result = 'detached')
        if state == 'attached':
            _update_floating_ip(neutron, module, None, floating_ip_id)
        module.exit_json(changed = True, result = "detached")
Esempio n. 8
0
def novaclient(cred):
    if cred.get("token", None) and cred.get("tenant_id", None):
        token = cred["token"]
        tenant_id = cred["tenant_id"]
    else:
        # Create scoped token for admin.
        unscoped_token = token_create(cred['username'], cred['password'])
        tenants = tenant_list_for_token(unscoped_token.id)
        tenant_id = tenants[0].id
        token = token_create(cred['username'], cred['password'], tenant_id)

    # Get service catalog
    catalog = service_catalog.ServiceCatalog(token)
    s_catalog = catalog.catalog.serviceCatalog

    management_url = url_for(s_catalog, 'compute')

    c = nova_client.Client(cred['username'],
                           token.id,
                           tenant_id,
                           management_url)
    c.client.auth_token = token.id
    c.client.management_url = management_url
    return c
Esempio n. 9
0
def client_conn(type_):
    if type_ == 'nova':
        client = client_nova.Client(username=AUTH_USER,
                                    insecure=True,
                                    api_key=AUTH_PASSWORD,
                                    project_id=AUTH_TENANT_NAME,
                                    auth_url=AUTH_URL,
                                    region_name=AUTH_REGION)
    elif type_ == 'keystone':
        client = client_keystone.Client(username=AUTH_USER,
                                        insecure=True,
                                        password=AUTH_PASSWORD,
                                        tenant_name=AUTH_TENANT_NAME,
                                        auth_url=AUTH_URL)
    elif type_ == 'cinder':
        client = client_cinder.Client(username=AUTH_USER,
                                      insecure=True,
                                      api_key=AUTH_PASSWORD,
                                      project_id=AUTH_TENANT_NAME,
                                      auth_url=AUTH_URL,
                                      region_name=AUTH_REGION,
                                      service_type='volume')

    return client
Esempio n. 10
0
def main():
    module = AnsibleModule(
        argument_spec                   = dict(
        login_username                  = dict(default='admin'),
        login_password                  = dict(required=True),
        login_tenant_name               = dict(required='True'),
        auth_url                        = dict(default='http://127.0.0.1:35357/v2.0/'),
        instance_name                   = dict(required=True),
        snapshot_name                   = dict(required=True),
        state                           = dict(default='present', choices=['absent', 'present']),
        wait_snapshot                         = dict(default='yes'),
        wait_for                    =  dict(default='7200')
        ),
    )

    nova = nova_client.Client(module.params['login_username'],
                              module.params['login_password'],
                              module.params['login_tenant_name'],
                              module.params['auth_url'],
                              service_type='compute')
    try:
        nova.authenticate()
    except exc.Unauthorized, e:
        module.fail_json(msg = "Invalid OpenStack Nova credentials.: %s" % e.message)
Esempio n. 11
0
def main():

    config = ConfigParser.ConfigParser()
    config.read('settings.ini')

    server_config = yaml.load(open('provision.cfg').read())

    nova = client.Client(config.get('provision', 'username'),
                         config.get('provision', 'password'),
                         None,
                         ENDPOINT,
                         region_name=config.get('provision', 'region'),
                         http_log_debug=1)

    idrsa = open(config.get('provision', 'idrsa')).readlines()[0]
    vm_password = config.get('provision', 'vm_password')

    # Provision any un-provisioned hosts
    provision(nova, server_config)
    wait_all_built(nova)
    tag_hosts(nova, server_config)
    set_password(nova, vm_password)
    print_server_ip(nova)
    add_ssh_key_all_hosts(nova, vm_password, idrsa)
Esempio n. 12
0
    def setUp(self):
        super(AkandaFunctionalBase, self).setUp()
        self.config = self._get_config()

        self.ak_cfg = mock.patch.object(akanda_client.cfg, 'CONF').start()
        self.ak_cfg.alive_timeout = 10
        self.ak_client = akanda_client

        self.novaclient = _novaclient.Client(
            self.config['os_username'],
            self.config['os_password'],
            self.config['os_tenant_name'],
            auth_url=self.config['os_auth_url'],
            auth_system='keystone',
        )

        self.neutronclient = _neutronclient.Client(
            username=self.config['os_username'],
            password=self.config['os_password'],
            tenant_name=self.config['os_tenant_name'],
            auth_url=self.config['os_auth_url'],
            auth_strategy='keystone',
        )
        self._management_address = None
Esempio n. 13
0
    def __init__(self, image_name, image_path):           

        """
        Constructor to create a Connection and an Image Instance.
    
        :param image_name: The name of the image to be created.
        :param image_path: The path of the image for creation.
        """

        logging.basicConfig(filename='openstack.log', level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' , datefmt='%m/%d/%Y %I:%M:%S %p')
        
        logging.info('Establishing Connection.')

        # Establishing Connections --------------------             
        try:
            # Connections with keystoneClient(identity API) --------------------
            creds = get_keystone_creds()
            self.keystone = ksclient.Client(**creds)
  
            # Connections with novaClient(compute API) --------------------    
            creds = get_nova_creds()
            self.nova = nvclient.Client(**creds)   
        except:
            logging.error('Connection not Established.')
            raise     

        # Creating IMAGE instance --------------------             
        try:
            logging.info('Creating Image.')
            self.is_image_present = False
            image = self.create(image_name, image_path)  
            self.image = image
        except:
            logging.error('Image not Created.')
            main.delete()
            raise     
Esempio n. 14
0
def novaclient(request):
    """
    insecure = getattr(settings, 'OPENSTACK_SSL_NO_VERIFY', False)
    cacert = getattr(settings, 'OPENSTACK_SSL_CACERT', None)
    LOG.debug('novaclient connection created using token "%s" and url "%s"' %
              (request.user.token.id, base.url_for(request, 'compute')))
    c = nova_client.Client(request.user.username,
                           request.user.token.id,
                           project_id=request.user.tenant_id,
                           auth_url=base.url_for(request, 'compute'),
                           insecure=insecure,
                           cacert=cacert,
                           http_log_debug=settings.DEBUG)
    c.client.auth_token = request.user.token.id
    c.client.management_url = base.url_for(request, 'compute')
    return c
    """

    c = nova_client.Client(username=request.get("username"),
                           api_key=request.get("password"),
                           project_id=request.get("tenant_name"),
                           auth_url=request.get("auth_url"),
                           http_log_debug=False)
    return c
Esempio n. 15
0
    def test_authenticate_success(self):
        cs = client.Client("username",
                           "password",
                           "project_id",
                           "auth_url",
                           no_cache=True)
        management_url = 'https://servers.api.rackspacecloud.com/v1.1/443470'
        auth_response = httplib2.Response({
            'status':
            204,
            'x-server-management-url':
            management_url,
            'x-auth-token':
            '1b751d74-de0c-46ae-84f0-915744b582d1',
        })
        mock_request = mock.Mock(return_value=(auth_response, None))

        @mock.patch.object(httplib2.Http, "request", mock_request)
        def test_auth_call():
            cs.client.authenticate()
            headers = {
                'Accept': 'application/json',
                'X-Auth-User': '******',
                'X-Auth-Key': 'password',
                'X-Auth-Project-Id': 'project_id',
                'User-Agent': cs.client.USER_AGENT
            }
            mock_request.assert_called_with(cs.client.auth_url,
                                            'GET',
                                            headers=headers)
            self.assertEqual(cs.client.management_url,
                             auth_response['x-server-management-url'])
            self.assertEqual(cs.client.auth_token,
                             auth_response['x-auth-token'])

        test_auth_call()
def main(args):
    credentials = getOsCredentialsFromEnvironment()
    nt = client.Client(credentials['USERNAME'],
                       credentials['PASSWORD'],
                       credentials['TENANT_NAME'],
                       credentials['AUTH_URL'],
                       service_type="compute")

    inventory = {}
    inventory['_meta'] = {'hostvars': {}}

    for server in nt.servers.list():
        groups = getAnsibleHostGroupsFromServer(nt, server)
        if len(groups) > 0:
            for group in groups:
                addServerToHostGroup(group, server, inventory)
        else:
            addServerToHostGroup('unassigned-group', server, inventory)

        host_vars = getAnsibleHostVarsFromServer(nt, server)
        if host_vars:
            addServerHostVarsToHostVars(host_vars, server, inventory)

    dumpInventoryAsJson(inventory)
Esempio n. 17
0
def create_server():
    # testing environment
    if 'True' == utils.get_config('mode', 'testing'):
        LOG.info('Now launching a private instance')

    # producting environment
    else:
        # TODO diy rest?
        import novaclient.v1_1.client as nvclient
        creds = get_nova_creds()
        nova = nvclient.Client(**creds)

        image = nova.images.find(name="cirros-0.3.2-x86_64")
        flavor = nova.flavors.find(name="m1.tiny")
        nics = [{'net-id': '80963cd2-0f29-433c-bb33-c4e25ad8719b'}]

        instance = nova.servers.create(name="bryant",
                                       image=image,
                                       flavor=flavor,
                                       nics=nics)
        LOG.info('Now launched a private instance')

        instance.add_floating_ip(get_floating_ip())
        LOG.info('Now adding floating ip to the private instance')
Esempio n. 18
0
    def verify(self, node, subject, **kwargs):
        try:
            CN = subject.CN
            OU = subject.OU

            keystone = k.Client(token=self._get_token(),
                                auth_url=self.ADMIN_AUTH_URL,
                                tenant_name=self.admin_tenant,
                                timeout=self.timeout)

            tenants = keystone.tenants.list()

            conn = n.Client(self.admin_user,
                            self.admin_pass,
                            self.admin_tenant,
                            self.ADMIN_AUTH_URL,
                            service_type="compute",
                            timeout=self.timeout)

            for server in conn.servers.list(True,
                                            search_opts={'all_tenants': True}):
                if self.strict_check:
                    # Perform strict check by hostname and ID
                    if server.name != CN:
                        continue
                if server.id == OU:
                    tenant = filter(lambda t: t.id == server.tenant_id,
                                    tenants)
                    if tenant:
                        name = tenant[0].name
                        LOG.info("Tenant [%s] matched for server %s" %
                                 (name, node))
                        return name
        except Exception, e:
            LOG.error(e)
            return None
Esempio n. 19
0
def main():

    argument_spec = openstack_argument_spec()
    argument_spec.update(
        dict(ip_address=dict(required=True),
             instance_name=dict(required=True),
             state=dict(default='present', choices=['absent', 'present'])))
    module = AnsibleModule(argument_spec=argument_spec)

    if not HAVE_DEPS:
        module.fail_json(
            msg=
            'python-novaclient, python-keystoneclient, and either python-neutronclient or python-quantumclient are required'
        )

    try:
        nova = nova_client.Client(module.params['login_username'],
                                  module.params['login_password'],
                                  module.params['login_tenant_name'],
                                  module.params['auth_url'],
                                  service_type='compute')
    except Exception, e:
        module.fail_json(msg=" Error in authenticating to nova: %s" %
                         e.message)
Esempio n. 20
0
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from nova import flags
from novaclient.v1_1 import client

FLAGS = flags.FLAGS

NOVA_CLIENT = client.Client(FLAGS.keystone_username,
                            FLAGS.keystone_password,
                            FLAGS.keystone_tenant_name,
                            FLAGS.keystone_auth_url,
                            service_type="compute")


def is_running(floating_ip_uuid, **kwargs):
    return not is_terminated(floating_ip_uuid)


def is_terminated(floating_ip_uuid):
    # TODO(lzyeval): handle error
    # NOTE(yaguang): handle exception when floating ip is released by user
    #                we can not get info from novaclient
    try:
        floating_ip = NOVA_CLIENT.floating_ips.get(floating_ip_uuid)
    except Exception:
Esempio n. 21
0
    def setUp(self):
        super(ITestCase, self).setUp()
        self.common_config = cfg.ITConfig().common_config
        self.vanilla_config = cfg.ITConfig().vanilla_config
        self.vanilla_two_config = cfg.ITConfig().vanilla_two_config
        self.hdp_config = cfg.ITConfig().hdp_config
        self.mapr_config = cfg.ITConfig().mapr_config
        self.mapr4_1_config = cfg.ITConfig().mapr4_1_config
        self.mapr4_2_config = cfg.ITConfig().mapr4_2_config

        telnetlib.Telnet(self.common_config.SAHARA_HOST,
                         self.common_config.SAHARA_PORT)

        keystone = keystone_client.Client(
            username=self.common_config.OS_USERNAME,
            password=self.common_config.OS_PASSWORD,
            tenant_name=self.common_config.OS_TENANT_NAME,
            auth_url=self.common_config.OS_AUTH_URL)

        keystone.management_url = self.common_config.OS_AUTH_URL

        self.common_config.OS_TENANT_ID = [
            tenant.id for tenant in keystone.tenants.list()
            if tenant.name == self.common_config.OS_TENANT_NAME
        ][0]

        self.sahara = sahara_client.Client(
            version=self.common_config.SAHARA_API_VERSION,
            username=self.common_config.OS_USERNAME,
            api_key=self.common_config.OS_PASSWORD,
            project_name=self.common_config.OS_TENANT_NAME,
            auth_url=self.common_config.OS_AUTH_URL,
            sahara_url='http://%s:%s/v%s/%s' %
            (self.common_config.SAHARA_HOST, self.common_config.SAHARA_PORT,
             self.common_config.SAHARA_API_VERSION,
             self.common_config.OS_TENANT_ID))

        self.nova = nova_client.Client(
            username=self.common_config.OS_USERNAME,
            api_key=self.common_config.OS_PASSWORD,
            project_id=self.common_config.OS_TENANT_NAME,
            auth_url=self.common_config.OS_AUTH_URL)

        self.neutron = neutron_client.Client(
            username=self.common_config.OS_USERNAME,
            password=self.common_config.OS_PASSWORD,
            tenant_name=self.common_config.OS_TENANT_NAME,
            auth_url=self.common_config.OS_AUTH_URL)

        if not self.common_config.FLAVOR_ID:
            self.flavor_id = self.nova.flavors.create(name='i-test-flavor-%s' %
                                                      str(uuid.uuid4())[:8],
                                                      ram=1024,
                                                      vcpus=1,
                                                      disk=10,
                                                      ephemeral=10).id

        else:
            self.flavor_id = self.common_config.FLAVOR_ID

        if not self.common_config.PATH_TO_SSH_KEY:
            self.common_config.USER_KEYPAIR_ID += str(uuid.uuid4())[:8]
            self.private_key = self.nova.keypairs.create(
                self.common_config.USER_KEYPAIR_ID).private_key

        else:
            self.private_key = open(self.common_config.PATH_TO_SSH_KEY).read()
os_user_name = config.get('OPENSTACK', 'os_user_name')
os_password = config.get('OPENSTACK', 'os_password')
os_tenant_name = config.get('OPENSTACK', 'os_tenant_name')
os_auth_url = config.get('OPENSTACK', 'os_auth_url')
os_region_name = config.get('OPENSTACK', 'os_region_name')

from neutronclient.v2_0 import client as neutronclient
nc = neutronclient.Client(username=os_user_name,
                   password=os_password,
                   tenant_name=os_tenant_name,
                   auth_url=os_auth_url)

from novaclient.v1_1 import client as novaclient
novac = novaclient.Client(os_user_name,
                      os_password,
                      os_tenant_name,
                      os_auth_url
                      )
from keystoneclient.v2_0 import client
keystone = client.Client(username=os_user_name,
                         password=os_password,
                         tenant_name=os_tenant_name,
                         auth_url=os_auth_url
                         )
glance_endpoint = keystone.service_catalog.get_endpoints('image').get('image')[0].get('internalURL')
## HACKITY HACK HACK HACK
if ( glance_endpoint.endswith('/v2') or glance_endpoint.endswith('/v1') ) :
  glance_endpoint = glance_endpoint.rsplit('/', 1)[0]

print "Found a glance endpoint at {}".format(glance_endpoint)
Esempio n. 23
0
def main():
    argument_spec = openstack_argument_spec()
    argument_spec.update(
        dict(
            name=dict(required=True),
            image_id=dict(default=None),
            image_name=dict(default=None),
            image_exclude=dict(default='(deprecated)'),
            flavor_id=dict(default=1),
            flavor_ram=dict(default=None, type='int'),
            flavor_include=dict(default=None),
            key_name=dict(default=None),
            security_groups=dict(default='default'),
            nics=dict(default=None, type='list'),
            meta=dict(default=None, type='dict'),
            wait=dict(default='yes', choices=['yes', 'no']),
            wait_for=dict(default=180),
            state=dict(default='present', choices=['absent', 'present']),
            user_data=dict(default=None),
            config_drive=dict(default=False, type='bool'),
            auto_floating_ip=dict(default=False, type='bool'),
            floating_ips=dict(default=None, type='list'),
            floating_ip_pools=dict(default=None, type='list'),
            scheduler_hints=dict(default=None, type='dict'),
        ))
    module = AnsibleModule(
        argument_spec=argument_spec,
        mutually_exclusive=[
            ['auto_floating_ip', 'floating_ips'],
            ['auto_floating_ip', 'floating_ip_pools'],
            ['floating_ips', 'floating_ip_pools'],
            ['image_id', 'image_name'],
            ['flavor_id', 'flavor_ram'],
        ],
    )

    if not HAS_NOVACLIENT:
        module.fail_json(msg='python-novaclient is required for this module')

    nova = nova_client.Client(module.params['login_username'],
                              module.params['login_password'],
                              module.params['login_tenant_name'],
                              module.params['auth_url'],
                              region_name=module.params['region_name'],
                              service_type='compute')
    try:
        nova.authenticate()
    except exceptions.Unauthorized as e:
        module.fail_json(msg="Invalid OpenStack Nova credentials.: %s" %
                         e.message)
    except exceptions.AuthorizationFailure as e:
        module.fail_json(msg="Unable to authorize user: %s" % e.message)

    if module.params['state'] == 'present':
        if not module.params['image_id'] and not module.params['image_name']:
            module.fail_json(
                msg=
                "Parameter 'image_id' or `image_name` is required if state == 'present'"
            )
        else:
            _get_server_state(module, nova)
            _create_server(module, nova)
    if module.params['state'] == 'absent':
        _get_server_state(module, nova)
        _delete_server(module, nova)
Esempio n. 24
0
'''

INSTANCE_NAME = 'fiva'
NETWORK_NAME='route-66'
SUBNET_CIDR='10.2.33.0/24'
INSTANCE_COUNT = 3

kcreds = get_keystone_creds()

print "Connecting to keystone"
keystone = keystoneclient.Client(**kcreds)
tokenlen=len(keystone.auth_token)
print keystone.auth_token[0:20] + "..." + keystone.auth_token[tokenlen-20:tokenlen]

ncreds = get_nova_creds()
nova = novaclient.Client(**ncreds)

flavors = nova.flavors.list(is_public=True)
print flavors

images = nova.images.list(detailed=False)
print images 

# get networks from quantum
print "Find or create network..."
network_url = keystone.service_catalog.url_for(service_type='network')
neutron = neutronclient.Client(endpoint_url=network_url, token=keystone.auth_token)
networks = neutron.list_networks()['networks']
print "Networks: "
print [(nw['name'],nw['id'])for nw in networks]
Esempio n. 25
0
    def snapshot_image_on_provider(self, builder, provider, credentials,
                                   target, template, parameters):
        self.log.info('snapshot_image_on_provider() called in Rackspace')

        self.builder = builder
        self.active_image = self.builder.provider_image

        # TODO: This is a convenience variable for refactoring - rename
        self.new_image_id = builder.provider_image.identifier
        # TODO: so is this
        self.target = target

        # Template must be defined for snapshots
        self.tdlobj = oz.TDL.TDL(xmlstring=str(template), rootpw_required=True)

        # Create a name combining the TDL name and the UUID for use when tagging Rackspace Images
        self.longname = self.tdlobj.name + "-" + self.new_image_id

        self.log.debug("Being asked to push for provider %s" % provider)
        self.log.debug(
            "distro: %s - update: %s - arch: %s" %
            (self.tdlobj.distro, self.tdlobj.update, self.tdlobj.arch))
        self.rackspace_decode_credentials(credentials)
        self.log.debug("acting as Rackspace user: %s" %
                       (str(self.rackspace_username)))

        self.status = "PUSHING"
        self.percent_complete = 0

        region = provider

        auth_url = 'https://identity.api.rackspacecloud.com/v2.0/'

        rackspace_client = client.Client(self.rackspace_username,
                                         self.rackspace_password,
                                         self.rackspace_account_number,
                                         auth_url,
                                         service_type="compute",
                                         region_name=region)
        rackspace_client.authenticate()

        mypub = open("/etc/oz/id_rsa-icicle-gen.pub")
        server_files = {"/root/.ssh/authorized_keys": mypub}

        # Now launch it
        self.activity("Launching Rackspace JEOS image")
        rackspace_image_id = self.rackspace_jeos_amis[region][
            self.tdlobj.distro][self.tdlobj.update][self.tdlobj.arch]['img_id']
        instance_type = '512MB Standard Instance'
        image = rackspace_client.images.find(id=rackspace_image_id)
        small = rackspace_client.flavors.find(name=instance_type)
        self.log.debug("Starting build server %s with instance_type %s" %
                       (rackspace_image_id, instance_type))
        reservation_name = 'imagefactory-snapshot-%s' % self.active_image.identifier
        reservation = rackspace_client.servers.create(reservation_name,
                                                      image,
                                                      small,
                                                      files=server_files)

        if not reservation:
            self.status = "FAILED"
            raise ImageFactoryException(
                "run_instances did not result in the expected single instance - stopping"
            )

        self.instance = reservation

        self.wait_for_rackspace_instance_start(self.instance)

        # From this point on we must be sure to terminate the instance when we are done
        # so wrap in a try/finally
        # Accidentally running a 64 bit instance doing nothing costs 56 USD week
        try:
            while self.instance.accessIPv4 == '':
                self.log.debug("Waiting to get public IP address")
            sleep(1)
            self.instance.get()
            guestaddr = self.instance.accessIPv4
            self.guest = oz.Fedora.FedoraGuest(self.tdlobj, self.oz_config,
                                               None, "virtio", True, "virtio",
                                               True)

            # Ugly ATM because failed access always triggers an exception
            self.wait_for_rackspace_ssh_access(guestaddr)

            # There are a handful of additional boot tasks after SSH starts running
            # Give them an additional 20 seconds for good measure
            self.log.debug("Waiting 60 seconds for remaining boot tasks")
            sleep(60)

            self.activity("Customizing running Rackspace JEOS instance")
            self.log.debug(
                "Stopping cron and killing any updatedb process that may be running"
            )
            # updatedb interacts poorly with the bundle step - make sure it isn't running
            self.guest.guest_execute_command(guestaddr,
                                             "/sbin/service crond stop")
            self.guest.guest_execute_command(
                guestaddr, "killall -9 updatedb || /bin/true")
            self.log.debug("Done")

            # Not all JEOS images contain this - redoing it if already present is harmless
            self.log.info("Creating cloud-info file indicating target (%s)" %
                          self.target)
            self.guest.guest_execute_command(
                guestaddr,
                'echo CLOUD_TYPE=\\\"%s\\\" > /etc/sysconfig/cloud-info' %
                self.target)

            self.log.debug("Customizing guest: %s" % guestaddr)
            self.guest.mkdir_p(self.guest.icicle_tmp)
            self.guest.do_customize(guestaddr)
            self.log.debug("Customization step complete")

            self.log.debug("Generating ICICLE from customized guest")
            self.output_descriptor = self.guest.do_icicle(guestaddr)
            self.log.debug("ICICLE generation complete")

            self.log.debug(
                "Re-de-activate firstboot just in case it has been revived during customize"
            )
            self.guest.guest_execute_command(
                guestaddr,
                "[ -f /etc/init.d/firstboot ] && /sbin/chkconfig firstboot off || /bin/true"
            )
            self.log.debug("De-activation complete")

            image_name = str(self.longname)
            #image_desc = "%s - %s" % (asctime(localtime()), self.tdlobj.description)

            self.log.debug(
                "Creating a snapshot of our running Rackspace instance")
            #TODO: give proper name??
            new_image_id = self.instance.create_image(image_name)
            new_image = rackspace_client.images.find(id=new_image_id)
            while True:
                new_image.get()
                self.log.info("Saving image: %d percent complete" %
                              new_image.progress)
                if new_image.progress == 100:
                    break
                else:
                    sleep(20)

            self.builder.provider_image.icicle = self.output_descriptor
            self.builder.provider_image.identifier_on_provider = new_image_id
            self.builder.provider_image.provider_account_identifier = self.rackspace_account_number
        except Exception as e:
            self.log.warning(
                "Exception while executing commands on guest: %s" % e)
        finally:
            self.terminate_instance(self.instance)
Esempio n. 26
0
import os,sys
import time

nombre1="servidor_web"
nombre2="servidor_mysql"
def get_nova_creds():
    d = {}
    d['username'] = os.environ['OS_USERNAME']
    d['api_key'] = os.environ['OS_PASSWORD']
    d['auth_url'] = os.environ['OS_AUTH_URL']
    d['project_id'] = os.environ['OS_TENANT_NAME']
    d['region_name']=os.environ['OS_REGION_NAME']
    return d

# Me conecto al cloud
nova = client.Client(**get_nova_creds())

#secgroup = nova.security_groups.find(name="default")
#nova.security_group_rules.delete(secgroup.id,ip_protocol="tcp",from_port="3128",to_port="3128",cidr="0.0.0.0/0")
for nomserver in [nombre1,nombre2]:

	server=nova.servers.find(name=nomserver)
	# Selecciono la ip flotente 
	ip_flotante =  server.networks.items()[0][1][1]
	# elimino la instancia
	server.delete()
	nova.floating_ips.delete(nova.floating_ips.find(ip=ip_flotante))
	print "Delete %s" % nomserver


Esempio n. 27
0
def openstack_launch(constellation_name, machine_name, constellation_directory,
                     user_data, nova_creds):
    '''
    Launches an openstack instance.
    Creates a unique keypair, security group, and floating ip
    and assigns them to the instance
    '''
    #nova_creds = get_nova_creds()
    nova = nvclient.Client(**nova_creds)
    #create keypair
    keypair_name = "key-%s-%s" % (machine_name, constellation_name)
    keypair = nova.keypairs.create(name=keypair_name)
    private_key = keypair.private_key
    path = os.path.join(constellation_directory, "%s.pem" % keypair_name)
    with open(path, 'w') as key_file:
        key_file.write(private_key)
    os.chmod(path, 0600)
    #create security group
    security_group_name = "security_" + constellation_name
    security_group = nova.security_groups.create(
        name=security_group_name,
        description="Security group for " + constellation_name)
    nova.security_group_rules.create(security_group.id, "TCP", 22, 22,
                                     "0.0.0.0/0")
    nova.security_group_rules.create(security_group.id, "TCP", 80, 80,
                                     "0.0.0.0/0")
    nova.security_group_rules.create(security_group.id, "ICMP", -1, -1,
                                     "0.0.0.0/0")
    #create instance
    instance_name = machine_name + "-" + constellation_name
    #image = nova.images.find(name="cirros-0.3.1-x86_64-uec")
    #flavor = nova.flavors.find(name="m1.tiny")
    image = nova.images.find(name="ubuntu12.04")
    flavor = nova.flavors.find(name="ubuntu")

    instance = nova.servers.create(name=instance_name,
                                   image=image,
                                   flavor=flavor,
                                   security_groups=[security_group.name],
                                   key_name=keypair_name,
                                   userdata=user_data)
    status = instance.status
    while status == 'BUILD':
        time.sleep(5)
        instance = nova.servers.get(instance.id)
        status = instance.status
    print("status: %s" % status)
    #assign_floating_ip
    instance = nova.servers.get(instance.id)
    flag = 0

    for floating_ip in nova.floating_ips.list():
        if floating_ip.instance_id is None:
            instance.add_floating_ip(floating_ip)
            flag = 1
            break
    if not flag:
        floating_ip = nova.floating_ips.create()
        instance.add_floating_ip(floating_ip)
    log("floating ip %s" % floating_ip)
    return floating_ip.ip, instance_name, keypair_name, security_group_name
Esempio n. 28
0
        '--nova-url',
        help='url for nova auth',
        default='http://openstack.eqiad1.wikimediacloud.org:35357/v2.0')
    argparser.add_argument('--nova-project',
                           help='project for nova auth',
                           default='admin')
    argparser.add_argument(
        'instanceid',
        help='instance id to migrate',
    )
    argparser.add_argument(
        'destination',
        help='destination host, e.g. labvirt1005',
    )
    args = argparser.parse_args()

    sshargs = [
        "ssh", "-i", "/root/.ssh/compute-hosts-key",
        "nova@%s.eqiad.wmnet" % args.destination, "true"
    ]
    r = subprocess.call(sshargs)
    if r:
        print("Remote execution failed; this whole enterprise is doomed.")
        exit(1)

    novaclient = client.Client(args.nova_user, args.nova_pass,
                               args.nova_project, args.nova_url)

    instance = NovaInstance(novaclient, args.instanceid)
    instance.migrate(args.destination)
Esempio n. 29
0
File: nova.py Progetto: s7726/salt
    def __init__(self,
                 username,
                 project_id,
                 auth_url,
                 region_name=None,
                 password=None,
                 os_auth_plugin=None,
                 **kwargs):
        '''
        Set up nova credentials
        '''
        if not HAS_NOVA:
            return None

        self.kwargs = kwargs.copy()
        self.kwargs['username'] = username
        self.kwargs['project_id'] = project_id
        self.kwargs['auth_url'] = auth_url
        self.kwargs['region_name'] = region_name
        self.kwargs['service_type'] = 'compute'
        if os_auth_plugin is not None:
            novaclient.auth_plugin.discover_auth_systems()
            auth_plugin = novaclient.auth_plugin.load_plugin(os_auth_plugin)
            self.kwargs['auth_plugin'] = auth_plugin
            self.kwargs['auth_system'] = os_auth_plugin

        if not self.kwargs.get('api_key', None):
            self.kwargs['api_key'] = password
        extensions = []
        if 'extensions' in kwargs:
            exts = []
            for key, item in self.kwargs['extensions'].items():
                mod = __import__(item.replace('-', '_'))
                exts.append(novaclient.extension.Extension(key, mod))
            self.kwargs['extensions'] = exts

        self.kwargs = sanatize_novaclient(self.kwargs)

        if not hasattr(client.Client, '__exit__'):
            raise SaltCloudSystemExit(
                "Newer version of novaclient required for __exit__.")

        with client.Client(**self.kwargs) as conn:
            try:
                conn.client.authenticate()
            except novaclient.exceptions.AmbiguousEndpoints:
                raise SaltCloudSystemExit(
                    "Nova provider requires a 'region_name' to be specified")

            self.kwargs['auth_token'] = conn.client.auth_token
            self.catalog = \
                conn.client.service_catalog.catalog['access']['serviceCatalog']

        if region_name is not None:
            servers_endpoints = get_entry(self.catalog, 'type',
                                          'compute')['endpoints']
            self.kwargs['bypass_url'] = get_entry(servers_endpoints, 'region',
                                                  region_name)['publicURL']

        self.compute_conn = client.Client(**self.kwargs)

        if region_name is not None:
            servers_endpoints = get_entry(self.catalog, 'type',
                                          'volume')['endpoints']
            self.kwargs['bypass_url'] = get_entry(servers_endpoints, 'region',
                                                  region_name)['publicURL']

        self.kwargs['service_type'] = 'volume'
        self.volume_conn = client.Client(**self.kwargs)
Esempio n. 30
0
    def test_ambiguous_endpoints(self):
        cs = client.Client("username",
                           "password",
                           "project_id",
                           "auth_url/v2.0",
                           service_type='compute')
        resp = {
            "access": {
                "token": {
                    "expires": "12345",
                    "id": "FAKE_ID",
                    "tenant": {
                        "id": "FAKE_TENANT_ID",
                    }
                },
                "serviceCatalog": [
                    {
                        "adminURL":
                        "http://*****:*****@mock.patch.object(requests, "request", mock_request)
        def test_auth_call():
            self.assertRaises(exceptions.AmbiguousEndpoints,
                              cs.client.authenticate)

        test_auth_call()