def prepare_devices(self): # Avoid adding duplicate entries in fstab mounted_devices = [] if os.path.isfile('/etc/fstab'): with open('/etc/fstab', 'r') as f: for l in f: mounted_devices.append(l.split()[0].strip()) # Add each device to fstab if not already added fstab = "" for device in self._devices: # Format the device with xfs filesystem util.ensure_directory('/srv/node/%s' % device['name']) # Ensure the device is mounted if device['device'] not in mounted_devices: fstab += ("{0} /srv/node/{1} xfs noatime,nodiratime,nobarrier," "logbufs=8 0 2\n").format(device['device'], device['name']) with open('/etc/fstab', 'a') as f: f.write(fstab) util.run_command('mount -a') # Change ownership of /srv to swift util.run_command("chown -R swift:swift /srv")
def start_server(self): LOG.debug("enabling rabbitmq-server service") self.enable_server() status = util.service_status("rabbitmq-server.service") LOG.debug("rabbitmq-server status: " + status) if status == "active": LOG.debug("rabbitmq-server service is already running") return elif status == "inactive" or status == "unknown": LOG.debug("starting rabbitmq-server service") util.run_command("systemctl restart rabbitmq-server") elif status == "failed": # try to re-run the service, catch the error if fails again try: LOG.debug("rabbitmq-server failed to start previously. " "Trying to start service...") util.run_command("systemctl restart rabbitmq-server") except: LOG.debug("rabbitmq-server.service failed to start." "Try 'rm -rf /var/lib/rabbitmq/*' and re-run" "clearstack") sys.exit(1) else: LOG.debug("Error: rabbitmq-server service status" " '{0}' unknown... Exiting clearstack".format(status)) sys.exit(1)
def setup_database(self, database, user, password): cmd = ("mongo --host 127.0.0.1 --eval '" "db = db.getSiblingDB(\"%s\");" "db.createUser({user: \"%s\",pwd: \"%s\"," "roles: [ \"readWrite\", \"dbAdmin\" ]})'" % (database, user, password)) util.run_command(cmd)
def create_role(self, role): LOG.debug("setting up %s role" % role) try: util.run_command("openstack role show %s" % role, environ=self._env) except: util.run_command("openstack role create %s" % role, environ=self._env)
def add_role(self, user, role, project="service", domain="default"): # openstack role add: error: argument --project: not allowed with # argument --domain cmd = "openstack role add --user {0} {1}".format(user, role) if domain != "default": cmd += " --domain %s" % domain else: cmd += " --project %s" % project util.run_command(cmd, environ=self._env)
def create_loopback(name, size): # Remove the file if exists if os.path.isfile(name): os.remove(name) # Create an empty file with open(name, "wb") as f: f.seek(1024 * 1024 * 1024 * size - 1) f.write(b"\0") util.run_command("mkfs.xfs %s" % name)
def sync_database(self): LOG.debug("populating neutron database") util.run_command('su -s /bin/sh -c "neutron-db-manage ' '--config-file /etc/neutron/neutron.conf ' '--config-file /etc/neutron/plugins/ml2/ml2_conf.ini ' 'upgrade head" neutron') if util.str2bool(CONF['CONFIG_LBAAS_INSTALL']): util.run_command('su -s /bin/sh -c "neutron-db-manage ' '--service lbaas upgrade head"')
def create_loopback(name, size): # Remove the file if exists if os.path.isfile(name): os.remove(name) # Create an empty file with open(name, 'wb') as f: f.seek(1024 * 1024 * 1024 * size - 1) f.write(b"\0") LOG.debug("formatting '{0}' as XFS".format(name)) util.run_command("mkfs.xfs %s" % name)
def create_project(self, project, description): LOG.debug("setting up %s project" % project) try: """ test if project already exists """ util.run_command("openstack project show %s" % project, environ=self._env) except: util.run_command( "openstack project create --domain default" ' --description "%s" %s' % (description, project), environ=self._env, )
def create_domain(self, domain, description): LOG.debug("setting up %s domain" % domain) try: """ test if domain already exists """ util.run_command("openstack domain show %s" % domain, environ=self._env) except: util.run_command("openstack domain create --description \"%s\" %s" % (description, domain), environ=self._env, debug=False)
def create_project(self, project, description): try: """ test if project already exists """ LOG.debug("checking if project '{0}' exists".format(project)) util.run_command("openstack project show %s" % project, environ=self._env) except: LOG.debug("setting up project '{0}'".format(project)) util.run_command("openstack project create --domain default" " --description \"%s\" %s" % (description, project), environ=self._env)
def create_network(self, name, network, public=None): cidr = IPv4Network(network) cidr_e = cidr.exploded cidr_gw = (cidr.network_address + 1).exploded cidr_start = (cidr.network_address + 2).exploded cidr_end = (cidr.broadcast_address - 1).exploded try: LOG.debug("checking if network '{0}' exists".format(name)) util.run_command("neutron net-show %s" % name, environ=self._env) except: LOG.debug("creating network '{0}'".format(name)) cmd = "neutron net-create %s" % name if public: cmd += (" --provider:physical_network public" " --shared --provider:network_type flat") util.run_command(cmd, environ=self._env) cmd = ("neutron subnet-create %s %s --name %s" " --gateway %s" % (name, cidr_e, name, cidr_gw)) if public: cmd += (" --allocation-pool start=%s,end=%s" % (cidr_start, cidr_end)) util.run_command(cmd, environ=self._env) if public: util.run_command("neutron net-update %s --router:external" % name, environ=self._env)
def create_service(self, name=None, description=None, type=None): name = name or self._name description = description or self._description type = type or self._type LOG.debug("setting up %s service" % name) try: """ test if service already exists """ util.run_command("openstack service show %s" % name, environ=self._env) except: util.run_command("openstack service create %s" " --name %s --description \"%s\"" % (type, name, description), environ=self._env)
def create_image(self, name, format, url, public=False): LOG.debug("Creating image %s" % name) try: util.run_command("openstack image show %s" % name, environ=self._env) except: command = ("openstack image create --disk-format %s %s" % (format, name)) if os.path.isfile(url): command += " --file %s" % url else: command += " --location %s" % url if public: command += " --public" util.run_command(command, environ=self._env)
def create_user(self, user=None, password=None, domain="default", project="service", role="admin"): user = user or self._user password = password or self._password LOG.debug("setting up %s user" % user) try: """ test if user already exists """ util.run_command("openstack user show %s" % user, environ=self._env) except: util.run_command("openstack user create --domain {0}" " --password={1}" " --email {2}@example.com {2}" .format(domain, password, user), environ=self._env, debug=False) self.add_role(user, role, project, domain)
def create_router(self, router, gw, interfaces): try: util.run_command("neutron router-show %s" % router, environ=self._env) except: util.run_command("neutron router-create %s" % router, environ=self._env) util.run_command("neutron router-gateway-set %s %s" % (router, gw), environ=self._env) for i in interfaces: util.run_command("neutron router-interface-add %s %s" % (router, i), environ=self._env)
def start_server(self): LOG.debug("starting services") util.run_command("systemctl enable mongodb") util.run_command("systemctl restart mongodb") count = 0 while count < 10: try: util.run_command("mongo --host 127.0.0.1") return except: util.run_command("sleep 1") count += 1 raise Exception("Failed to start mongodb service")
def create_router(self, router, gw, interfaces): try: LOG.debug("checking if router '{0}' exists".format(router)) util.run_command("neutron router-show %s" % router, environ=self._env) except: LOG.debug("creating router '{0}'".format(router)) util.run_command("neutron router-create %s" % router, environ=self._env) util.run_command("neutron router-gateway-set %s %s" % (router, gw), environ=self._env) for i in interfaces: util.run_command("neutron router-interface-add %s %s" % (router, i), environ=self._env)
def user_exists(self, user): stdout = None stderr = None try: stdout, stderr = util.run_command("rabbitmqctl list_users" " | grep {0}".format(user)) except: pass if not stdout: return False return True
def start_server(self, services=None): services = services or self._services LOG.debug("Starting services") util.run_command("systemctl enable %s" % " ".join(services)) for service in services: if service.endswith(".socket"): util.run_command("systemctl stop %s" % service.replace(".socket", ".service")) util.run_command("systemctl restart %s" % service)
def user_exists(self, user): stdout = None stderr = None try: LOG.debug("Checking if rabbitmq user '{0}' exists".format(user)) stdout, stderr = util.run_command("rabbitmqctl list_users" " | grep {0}".format(user)) except: pass if not stdout: LOG.debug("rabbitmq user '{0}' does not exists".format(user)) return False LOG.debug("rabbitmq user '{0}' does exists".format(user)) return True
def install(bundle): if not os.path.isfile("/usr/share/clear/bundles/" + str(bundle)) and \ not os.path.isfile("/usr/share/clear/bundles/" "openstack-all-in-one"): LOG.info("Installing {0} bundle".format(bundle)) cmd = "clr_bundle_add -V {0}".format(bundle) if(os.path.isfile("/bin/swupd")): cmd = "swupd bundle-add -V {0}".format(bundle) try: stdout, stderr = util.run_command(cmd) if stderr: LOG.error("swupd bundle-add: {0}\n{1}" .format(stdout, stderr)) except Exception: LOG.error("clearstack: cannot install" " {0} bundle".format(bundle))
def setup_database(self, database, user, password): LOG.debug("setting up '{0}' database".format(database)) mariadb_user = CONF["CONFIG_MARIADB_USER"] mariadb_pw = CONF["CONFIG_MARIADB_PW"] util.run_command("mysql -u{0} -p{1} -e" " \"CREATE DATABASE if not exists {2};\"" .format(mariadb_user, mariadb_pw, database), debug=False) util.run_command("mysql -u{0} -p{1} -e" " \"GRANT ALL PRIVILEGES ON {2}.* TO" " '{3}'@'localhost' IDENTIFIED BY '{4}';\"" .format(mariadb_user, mariadb_pw, database, user, password), debug=False) util.run_command("mysql -u{0} -p{1} -e" " \"GRANT ALL PRIVILEGES ON {2}.* TO '{3}'@'%'" " IDENTIFIED BY '{4}';\"" .format(mariadb_user, mariadb_pw, database, user, password), debug=False)
def create_rings(self): replicas = CONF["CONFIG_SWIFT_STORAGE_REPLICAS"] ip = util.get_ip() for ringtype, port in [("object", "6000"), ("container", "6001"), ("account", 6002)]: cmd = "swift-ring-builder {0}.builder create 10 {1} 1".format(ringtype, replicas) util.run_command(cmd) for device in self._devices: cmd = ( "swift-ring-builder %s.builder add --region 1 " "--zone %s --ip %s --port %s --device %s --weight 100" % (ringtype, device["zone"], ip, port, device["name"]) ) util.run_command(cmd) cmd = "swift-ring-builder {0}.builder rebalance".format(ringtype) util.run_command(cmd) if os.path.isfile("/etc/swift/%s.ring.gz" % ringtype): os.remove("/etc/swift/%s.ring.gz" % ringtype) shutil.move("%s.ring.gz" % ringtype, "/etc/swift")
def create_endpoint(self, publicurl=None, internalurl=None, adminurl=None, region=None, type=None): publicurl = publicurl or self._public_url internalurl = internalurl or self._internal_url adminurl = adminurl or self._admin_url region = region or self._region type = type or self._type LOG.debug("creating endpoint") """ test if endpoint already exists """ out, err = util.run_command("openstack endpoint list | grep %s" % self._name, environ=self._env) if not out: cmd = ("openstack endpoint create --region {0} {1}" .format(region, type)) util.run_command(cmd + " public {0}".format(publicurl), environ=self._env) util.run_command(cmd + " internal {0}".format(internalurl), environ=self._env) util.run_command(cmd + " admin {0}".format(adminurl), environ=self._env)
def create_rings(self): replicas = CONF['CONFIG_SWIFT_STORAGE_REPLICAS'] ip = util.get_ip() for ringtype, port in [('object', '6000'), ('container', '6001'), ('account', 6002)]: LOG.debug("creating '{0}' ring with {1} " "replicas".format(ringtype, replicas)) cmd = ("swift-ring-builder {0}.builder create 10 {1} 1" .format(ringtype, replicas)) util.run_command(cmd) for device in self._devices: LOG.debug("adding '{0}' storage node on ring " "'{1}'".format(device['name'], ringtype)) cmd = ("swift-ring-builder %s.builder add --region 1 " "--zone %s --ip %s --port %s --device %s --weight 100" % (ringtype, device['zone'], ip, port, device['name'])) util.run_command(cmd) LOG.debug("rebalancing ring '{0}'".format(ringtype)) cmd = "swift-ring-builder {0}.builder rebalance".format(ringtype) util.run_command(cmd) if os.path.isfile("/etc/swift/%s.ring.gz" % ringtype): os.remove("/etc/swift/%s.ring.gz" % ringtype) shutil.move("%s.ring.gz" % ringtype, "/etc/swift")
def secure_installation(self, user, password): LOG.debug("calling mariadb secure installation") try: """ test if mysql has a password """ util.run_command("mysql -u{0} -e \"\"".format(user), debug=False) """ Secure the database service """ util.run_command('mysqladmin -u root password "{0}"' .format(password, debug=False)) util.run_command('mysql -u root -p"{0}" -e "UPDATE mysql.user ' 'SET Password=PASSWORD(\'{0}\') ' 'WHERE User=\'root\'"' .format(password, debug=False)) util.run_command('mysql -u root -p"{0}" -e "DELETE FROM ' 'mysql.user WHERE User=\'root\' AND Host ' ' NOT IN (\'localhost\', \'127.0.0.1\', ' '\'::1\')"' .format(password, debug=False)) util.run_command('mysql -u root -p"{0}" -e "DELETE FROM ' 'mysql.user WHERE User=\'\'"' .format(password, debug=False)) util.run_command('mysql -u root -p"{0}" -e "DELETE FROM mysql.db ' 'WHERE Db=\'test\' OR Db=\'test\_%\'"' .format(password, debug=False)) util.run_command('mysql -u root -p"{0}" -e ' '"FLUSH PRIVILEGES"' .format(password, debug=False)) except: pass try: """ verify the password """ util.run_command("mysql -u{0} -p{1} -e \"\"" .format(user, password), debug=False) except: LOG.error("clearstack: cannot connect to mysql database," " the password is incorrect") return sys.exit(1)
def start_server(self): LOG.debug("starting mariadb service") util.run_command("systemctl enable mariadb") util.run_command("systemctl restart mariadb")
# limitations under the License. # from modules.conf import CONF from modules.keystone import Keystone from common import util keystone = Keystone.get() config_file = "/etc/keystone/keystone.conf" keystone.install() keystone.config_debug(config_file) keystone.config_database(config_file) keystone.config_admin_token(config_file) util.run_command("systemctl restart update-triggers.target") keystone.sync_database() keystone.start_server() keystone.create_service() keystone.create_endpoint() keystone.create_project("admin", "Admin Project") keystone.create_role("admin") keystone.create_user(user="******", project="admin", role="admin") keystone.create_project("service", "Service Project") keystone.create_project("demo", "Demo Project") keystone.create_role("user") keystone.create_user(user="******", project="demo", role="user", password=CONF['CONFIG_KEYSTONE_DEMO_PW']) util.delete_option(config_file, "DEFAULT", "admin_token") keystone.start_server()
def sync_database(self): LOG.debug("populating keystone database") util.run_command("su -s /bin/sh -c" " \"keystone-manage db_sync\" keystone")