Exemplo n.º 1
0
def action_list():
    ksclient = Keystone(options.config, debug=options.debug, log=logger)
    status = 'deactivated' if options.deactive else 'active'
    filters = {'status': status, 'visibility': options.visibility, 'tag': tags}
    logger.debug('=> filter: %s' % filters)
    images = glclient.get_images(filters=filters)
    out_image = {'header': 'Image list (id, name, created_at)'}
    if options.format == 'text':
        printer.output_dict(out_image)
    count = 0
    for image in images:
        out_image = {'name': image.name, 'created': image.created_at, 'id': image.id}
        if options.detailed and image.visibility == 'private':
            access = glclient.get_access(image.id)
            if access:
                access_list = list()
                for member in access:
                    project = ksclient.get_by_id('project', member['member_id'])
                    if project:
                        access_list.append(project.name)
                out_image['projects'] = access_list
        if options.detailed and hasattr(image, 'depricated'):
            out_image['depricated'] = image.depricated
        if options.detailed:
            out_image['tags'] = image.tags
        one_line = False if options.detailed else True
        printer.output_dict(out_image, sort=True, one_line=one_line)
        count += 1
    out_image = {'header': 'Image count', 'count': count}
    printer.output_dict(out_image)
Exemplo n.º 2
0
def action_grant():
    print "WARNING: this will break with the upgrade to ocata"
    print "https://trello.com/c/vjRI4EKC/"
    ksclient = Keystone(options.config, debug=options.debug, log=logger)
    project = ksclient.get_project(project=options.project, domain=options.domain)
    if not project:
        himutils.sys_error('Unknown project %s in domain %s' %
                           (options.project, options.domain))
    if options.name:
        tags.append(options.name)
    filters = {'status': 'active', 'tag': tags, 'visibility': 'private'}
    logger.debug('=> filter: %s' % filters)
    images = glclient.get_images(filters=filters)
    for image in images:
        log_msg = 'grant access to %s from %s' % (image.name, project.name)
        if not options.dry_run:
            glclient.set_access(image.id, project.id)
        else:
            log_msg = 'DRY-RUN: %s' % log_msg
        logger.debug('=> %s' % log_msg)
Exemplo n.º 3
0
from himlarcli.keystone import Keystone
from himlarcli.nova import Nova
#from himlarcli.cinder import Cinder
from himlarcli.gnocchi import Gnocchi
from himlarcli.cinder import Cinder
from himlarcli.parser import Parser
from himlarcli.printer import Printer
from himlarcli import utils as himutils
from datetime import date, timedelta
from collections import OrderedDict

parser = Parser()
options = parser.parse_args()
printer = Printer(options.format)

kc= Keystone(options.config, debug=options.debug)
kc.set_domain(options.domain)
kc.set_dry_run(options.dry_run)
logger = kc.get_logger()

# Billing will always use all regions
regions = kc.find_regions()

def action_whales():
    start = himutils.get_date(options.start, date.today() - timedelta(days=1))
    stop = himutils.get_date(options.end, date.today() + timedelta(days=1))
    if start > stop:
        himutils.sys_error('start %s must be fore stop %s' % (start, stop))
    logger.debug('=> start date = %s', start)
    logger.debug('=> stop date = %s', stop)
Exemplo n.º 4
0
# OPS! It might need some updates. We use class Mail instead of Notify now.

himutils.is_virtual_env()

# Default value for date: today + 5 days at 14:00
today = datetime.today()
date = datetime(today.year, today.month, today.day, 15, 0) + timedelta(days=5)

# Load parser config from config/parser/*
parser = Parser()
parser.update_default('-m', date.strftime('%Y-%m-%d around %H:00'))
options = parser.parse_args()
printer = Printer(options.format)

ksclient = Keystone(options.config, debug=options.debug)
logger = ksclient.get_logger()
novaclient = Nova(options.config, debug=options.debug, log=logger)
domain = 'Dataporten'
zone = '%s-default-1' % ksclient.region
msg_file = 'notify/notify_reboot.txt'

# aggregate in <loc>-legacy-1 AZ
legacy_aggregate = ['group1', 'group2', 'group3']

if 'host' in options and options.host:
    if '.' in options.host:
        host = options.host
    else:
        domain = ksclient.get_config('openstack', 'domain')
        host = options.host + '.' + domain
Exemplo n.º 5
0
from himlarcli import tests as tests
tests.is_virtual_env()

from himlarcli.keystone import Keystone
from himlarcli.nova import Nova
from himlarcli.parser import Parser
from himlarcli.printer import Printer
from himlarcli import utils as himutils
import time

parser = Parser()
options = parser.parse_args()
printer = Printer(options.format)

kc = Keystone(options.config, debug=options.debug)
kc.set_dry_run(options.dry_run)
logger = kc.get_logger()
nc = Nova(options.config, debug=options.debug, log=logger)
nc.set_dry_run(options.dry_run)


def action_instances():
    host = nc.get_host(nc.get_fqdn(options.host))
    if not host:
        himutils.sys_error('Could not find valid host %s' % options.host)
    search_opts = dict(all_tenants=1, host=host.hypervisor_hostname)
    instances = nc.get_all_instances(search_opts=search_opts)
    printer.output_dict({'header': 'Instance list (id, name, status, updated)'})
    status = dict({'total': 0})
    for i in instances:
Exemplo n.º 6
0
#!/usr/bin/env python

from himlarcli.keystone import Keystone
from himlarcli.nova import Nova
from himlarcli.cinder import Cinder
from himlarcli.parser import Parser
from himlarcli.printer import Printer
from himlarcli import utils as himutils

himutils.is_virtual_env()

parser = Parser()
options = parser.parse_args()
printer = Printer(options.format)

ksclient = Keystone(options.config, debug=options.debug)
ksclient.set_dry_run(options.dry_run)
logger = ksclient.get_logger()

if hasattr(options, 'region'):
    regions = ksclient.find_regions(region_name=options.region)
else:
    regions = ksclient.find_regions()

if not regions:
    himutils.sys_error('no valid regions found!')

def action_volume():
    projects = ksclient.get_projects(domain=options.domain)
    for region in regions:
        cinderclient = Cinder(options.config, debug=options.debug, log=logger, region=region)
Exemplo n.º 7
0
#!/usr/bin/env python
import utils
from himlarcli.keystone import Keystone
from himlarcli.foremanclient import ForemanClient
from himlarcli import utils as himutils

# Fix foreman functions and logger not-callable
# pylint: disable=E1101,E1102

desc = 'Setup Foreman for himlar'
options = utils.get_options(desc, hosts=False)
keystone = Keystone(options.config, debug=options.debug)
logger = keystone.get_logger()
domain = keystone.get_config('openstack', 'domain')

foreman = ForemanClient(options.config, options.debug, log=logger)
client = foreman.get_client()

# create foreman domain based on config
# get domain id

# get smart proxy id for tftp

# create subnet
# mgmt network + netmask from config
# domain_ids = domain_id
# tftp_ids = proxy_id
# dns-primary, dns-secondary, gateway is blank
# get subnet id

# Glabal parameters
Exemplo n.º 8
0
#!/usr/bin/env python
""" Setup dataporten openid mapping """

import utils
from himlarcli.keystone import Keystone
from himlarcli import utils as himutils

options = utils.get_options('Setup dataporten openid mapping',
                             hosts=0, dry_run=True)
ksclient = Keystone(options.config, debug=options.debug)
ksclient.set_domain('dataporten')
# Domain should be create from hieradata
domain = ksclient.get_domain_id()
rules = [{
    "local": [{
        "user": { "name": "{0}", "id": "{0}" },
        "group": { "name": "{0}-group", "domain": { "id": domain } } }],
    "remote": [{ "type": "OIDC-email" }, { "type": "OIDC-name" }]
}, {
    "local": [{
        "group": { "name": "nologin", "domain": { "id": domain } } }],
        "remote": [{ "type": "OIDC-email" }, { "type": "OIDC-name" }]
}]

# Crate nologin group
desc = 'All authenticated users are mapped to nologin which has no role grants'
ksclient.create_group('nologin', desc, 'dataporten')

# Create provider, mapping and container to connect them
ksclient.set_mapping('dataporten_personal', rules)
ksclient.set_protocol('openid', 'dataporten', 'dataporten_personal')
Exemplo n.º 9
0
#!/usr/bin/env python
import sys
from himlarcli.keystone import Keystone
from himlarcli.nova import Nova
from himlarcli.parser import Parser
from himlarcli.printer import Printer
from himlarcli import utils as himutils

himutils.is_virtual_env()

# Load parser config from config/parser/*
parser = Parser()
options = parser.parse_args()

ksclient = Keystone(options.config, debug=options.debug)
ksclient.set_domain('Dataporten')
logger = ksclient.get_logger()
printer = Printer(options.format)

# Regions
regions = himutils.load_region_config('config/stats',
                                      region=ksclient.region,
                                      log=logger)
""" ACTIONS """

def project():
    stats = {'demo': 0,
             'personal': 0,
             'research': 0,
             'education': 0,
             'admin': 0,
Exemplo n.º 10
0
#!/usr/bin/python
import sys
import utils
from himlarcli.nova import Nova
from himlarcli.keystone import Keystone
from himlarcli.mail import Mail
from email.mime.text import MIMEText

# OPS! It might need some updates. We use class Mail instead of Notify now.

options = utils.get_options('Notify all users', dry_run=True, hosts=False)
keystone = Keystone(options.config, debug=options.debug)
mail = Mail(options.config, debug=options.debug)
region = keystone.region

print "Remove these lines if you want to run this and send mail to all!"
sys.exit(0)

# Edit this to send new email to all users
subject = 'UH-IaaS: Purge of all data (%s)' % region
body_file = 'notify/notify_reinstall.txt'

with open(body_file, 'r') as body_txt:
    body_content = body_txt.read()

projects = keystone.list_projects('Dataporten')
for project in projects:
    msg = MIMEText(body_content)
    msg['Subject'] = subject
    if not options.dry_run:
        mail.send_mail(project, msg)
Exemplo n.º 11
0
from himlarcli.printer import Printer
from himlarcli.ldapclient import LdapClient
from himlarcli.mail import Mail
from himlarcli import utils as himutils
from datetime import datetime
import time

# OPS! It might need some updates. We use class Mail instead of Notify now.

himutils.is_virtual_env()

# Load parser config from config/parser/*
parser = Parser()
options = parser.parse_args()

ksclient = Keystone(options.config, debug=options.debug)
ksclient.set_dry_run(options.dry_run)
ksclient.set_domain(options.domain)
logger = ksclient.get_logger()
printer = Printer(options.format)


def action_show():
    if not ksclient.is_valid_user(email=options.user, domain=options.domain):
        print "%s is not a valid user. Please check your spelling or case." % options.user
        sys.exit(1)
    obj = ksclient.get_user_objects(email=options.user, domain=options.domain)
    obj_type = options.obj_type
    if obj_type not in obj:
        return
    if obj_type == 'projects':
Exemplo n.º 12
0
from himlarcli.keystone import Keystone
from himlarcli.cinder import Cinder
from himlarcli.nova import Nova
from himlarcli.neutron import Neutron
from himlarcli.parser import Parser
from himlarcli.printer import Printer
from himlarcli import utils as himutils

himutils.is_virtual_env()

parser = Parser()
options = parser.parse_args()
printer = Printer(options.format)

ksclient = Keystone(options.config, debug=options.debug)
logger = ksclient.get_logger()
regions = ksclient.find_regions(region_name=options.region)

if not regions:
    himutils.sys_error('no regions found!')

def action_show():
    for region in regions:
        novaclient = Nova(options.config, debug=options.debug, log=logger, region=region)
        cinderclient = Cinder(options.config, debug=options.debug, log=logger, region=region)
        components = {'nova': novaclient, 'cinder': cinderclient}
        for comp, client in components.iteritems():
            if options.service != 'all' and comp != options.service:
                continue
            if hasattr(client, 'get_quota_class'):
Exemplo n.º 13
0
import time
from himlarcli.keystone import Keystone
from himlarcli.nova import Nova
from himlarcli.parser import Parser
from himlarcli.printer import Printer
from himlarcli import utils as himutils
from himlarcli.notify import Notify

# OPS! It might need some updates. We use class Mail instead of Notify now.

himutils.is_virtual_env()

parser = Parser()
options = parser.parse_args()

ksclient = Keystone(options.config, debug=options.debug)
logger = ksclient.get_logger()
printer = Printer(options.format)

# Project type
project_types = himutils.load_config('config/type.yaml', log=logger)
if options.type and options.type not in project_types['types']:
    sys.stderr.write("Project type %s not valid. See config/type.yaml\n"
                     % options.type)
    sys.exit(1)

# Region
regions = list()
if options.region:
    regions = regions.append(options.region)
else:
Exemplo n.º 14
0
import json
from himlarcli import tests as tests
tests.is_virtual_env()

from himlarcli.keystone import Keystone
from himlarcli.parser import Parser
from himlarcli.mqclient import MQclient
from himlarcli.printer import Printer
from himlarcli import utils as himutils

parser = Parser()
options = parser.parse_args()
printer = Printer(options.format)

ksclient = Keystone(options.config, debug=options.debug)
ksclient.set_dry_run(options.dry_run)
ksclient.set_domain('dataporten')
logger = ksclient.get_logger()
mqclient = MQclient(options.config, debug=options.debug, log=logger)
mqclient.set_dry_run(options.dry_run)

# pylint: disable=W0613
def process_action(ch, method, properties, body): #callback
    ch.basic_ack(delivery_tag=method.delivery_tag)

    data = json.loads(body)
    user = ksclient.get_user_by_email(data['email'], user_type='api')

    if user:
        if data['action'] == 'reset_password':
Exemplo n.º 15
0
from himlarcli.keystone import Keystone
from himlarcli.nova import Nova
from himlarcli.parser import Parser
from himlarcli.printer import Printer
from himlarcli import utils as himutils
from himlarcli.mail import Mail
from email.mime.text import MIMEText


himutils.is_virtual_env()

parser = Parser()
options = parser.parse_args()
printer = Printer(options.format)

ksclient = Keystone(options.config, debug=options.debug)
logger = ksclient.get_logger()

# Update these before running the script
emails_file = 'notify/user_emails.txt'
content = 'notify/mailto_all.txt'
subject = 'INFO UH-IaaS'

if hasattr(options, 'region'):
    regions = ksclient.find_regions(region_name=options.region)
else:
    regions = ksclient.find_regions()

if not regions:
    himutils.sys_error('no valid regions found!')
Exemplo n.º 16
0
def action_create():
    if not ksclient.is_valid_user(options.admin, options.domain) and options.type == 'personal':
        himutils.sys_error('not valid user', 1)
    quota = himutils.load_config('config/quotas/%s.yaml' % options.quota)
    if options.quota and not quota:
        himutils.sys_error('Could not find quota in config/quotas/%s.yaml' % options.quota)
    test = 1 if options.type == 'test' else 0
    if options.enddate:
        try:
            enddate = datetime.strptime(options.enddate, '%d.%m.%Y').date()
        except ValueError:
            himutils.sys_error('date format DD.MM.YYYY not valid for %s' % options.enddate, 1)
    else:
        enddate = None
    createdate = datetime.today()
    if not options.force:
        print 'Project name: %s\nAdmin: %s\nType: %s\nEnd date: %s\nQuota: %s\nRT: %s' \
                % (options.project,
                   options.admin.lower(),
                   options.type,
                   str(enddate),
                   options.quota,
                   options.rt)
        if not himutils.confirm_action('Are you sure you want to create this project?'):
            himutils.sys_error('Aborted', 1)
    project = ksclient.create_project(project_name=options.project,
                                      admin=options.admin.lower(),
                                      test=test,
                                      type=options.type,
                                      description=options.desc,
                                      enddate=str(enddate),
                                      createdate=createdate.isoformat(),
                                      quota=options.quota,
                                      rt=options.rt)
    if not ksclient.is_valid_user(options.admin, options.domain):
        himutils.sys_error('WARNING: "%s" is not a valid user.' % options.admin, 0)
    if not project:
        himutils.sys_error('Failed creating %s' % options.project, 1)
    else:
        output = Keystone.get_dict(project)
        output['header'] = "Show information for %s" % options.project
        printer.output_dict(output)

    # Quotas
    for region in regions:
        novaclient = Nova(options.config, debug=options.debug, log=logger, region=region)
        cinderclient = Cinder(options.config, debug=options.debug, log=logger, region=region)
        neutronclient = Neutron(options.config, debug=options.debug, log=logger, region=region)
        cinderclient.set_dry_run(options.dry_run)
        novaclient.set_dry_run(options.dry_run)
        neutronclient.set_dry_run(options.dry_run)
        project_id = Keystone.get_attr(project, 'id')
        if quota and 'cinder' in quota and project:
            cinderclient.update_quota(project_id=project_id, updates=quota['cinder'])
        if quota and 'nova' in quota and project:
            novaclient.update_quota(project_id=project_id, updates=quota['nova'])
        if quota and 'neutron' in quota and project:
            neutronclient.update_quota(project_id=project_id, updates=quota['neutron'])

    if options.mail:
        mail = Mail(options.config, debug=options.debug)
        mail.set_dry_run(options.dry_run)

        if options.rt is None:
            himutils.sys_error('--rt parameter is missing.')
        else:
            mapping = dict(project_name=options.project,
                           admin=options.admin.lower(),
                           quota=options.quota,
                           end_date=str(enddate))
            subject = 'UH-IaaS: Project %s has been created' % options.project
            body_content = himutils.load_template(inputfile=project_msg_file,
                                                  mapping=mapping)
        if not body_content:
            himutils.sys_error('ERROR! Could not find and parse mail body in \
                               %s' % options.msg)

        mime = mail.rt_mail(options.rt, subject, body_content)
        mail.send_mail('*****@*****.**', mime)