Moving some of env_gen to the main devstack code.

This commit is contained in:
Joshua Harlow
2012-02-07 16:15:34 -08:00
parent 19cde481ea
commit 809c3f644a
4 changed files with 156 additions and 303 deletions

148
devstack/env_rc.py Normal file
View File

@@ -0,0 +1,148 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright (C) 2012 Yahoo! Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# 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.
import os
import re
import subprocess
import sys
from devstack import cfg
from devstack import shell as sh
from devstack import utils
from devstack.progs import common
CFG_MAKE = {
'ADMIN_PASSWORD': ('passwords', 'horizon_keystone_admin'),
'MYSQL_PASSWORD': ('passwords', 'sql'),
'RABBIT_PASSWORD': ('passwords', 'rabbit'),
'SERVICE_TOKEN': ('passwords', 'service_token'),
'FLAT_INTERFACE': ('nova', 'flat_interface'),
'HOST_IP': ('host', 'ip'),
}
#various settings we will output
EC2_PORT = 8773
S3_PORT = 3333
#these are pretty useless
EC2_USER_ID = 42
EC2_ACCESS_KEY = 'demo'
#change if you adjust keystone
NOVA_PORT = 5000
NOVA_VERSION = '1.1'
NOVA_PROJECT = 'demo'
NOVA_REGION = 'RegionOne'
#change if you adjust keystone
OS_TENANT_NAME = 'demo'
OS_USERNAME = 'demo'
OS_AUTH_PORT = 5000
EXP_PAT = re.compile("^export (.*?)=(.*?)$", re.IGNORECASE)
def _write_line(text, fh):
fh.write(text)
fh.write(os.linesep)
def _write_env(name, value, fh):
str_value = str(value)
escaped_val = subprocess.list2cmdline([str_value])
if str_value != escaped_val:
_write_line("export %s=\"%s\"" % (name, escaped_val), fh)
else:
_write_line("export %s=%s" % (name, str_value), fh)
def _generate_ec2_env(fh, cfg):
_write_line('# EC2 and/or S3 stuff', fh)
ip = cfg.get('host', 'ip')
_write_env('EC2_URL', 'http://%s:%s/services/Cloud' % (ip, EC2_PORT), fh)
_write_env('S3_URL', 'http://%s:%s/services/Cloud' % (ip, S3_PORT), fh)
_write_env('EC2_ACCESS_KEY', EC2_ACCESS_KEY, fh)
hkpw = cfg.get('passwords', 'horizon_keystone_admin', auto_pw=False)
if hkpw:
_write_env('EC2_SECRET_KEY', hkpw, fh)
_write_env('EC2_USER_ID', EC2_USER_ID, fh)
_write_env('EC2_CERT', '~/cert.pem', fh)
_write_line("", fh)
def _generate_nova_env(fh, cfg):
_write_line('# Nova stuff', fh)
ip = cfg.get('host', 'ip')
hkpw = cfg.get('passwords', 'horizon_keystone_admin', auto_pw=False)
if hkpw:
_write_env('NOVA_PASSWORD', hkpw, fh)
_write_env('NOVA_URL', 'http://%s:%s/v2.0' % (ip, NOVA_PORT), fh)
_write_env('NOVA_PROJECT_ID', NOVA_PROJECT, fh)
_write_env('NOVA_REGION_NAME', NOVA_REGION, fh)
_write_env('NOVA_VERSION', NOVA_VERSION, fh)
_write_env('NOVA_CERT', '~/cacert.pem', fh)
_write_line("", fh)
def _generate_os_env(fh, cfg):
_write_line('# Openstack stuff', fh)
ip = cfg.get('host', 'ip')
hkpw = cfg.get('passwords', 'horizon_keystone_admin', auto_pw=False)
if hkpw:
_write_env('OS_PASSWORD', hkpw, fh)
_write_env('OS_TENANT_NAME', OS_TENANT_NAME, fh)
_write_env('OS_USERNAME', OS_USERNAME, fh)
_write_env('OS_AUTH_URL', 'http://%s:%s/v2.0' % (ip, OS_AUTH_PORT), fh)
_write_line("", fh)
def generate_local_rc(fn=None, cfg=None):
if not fn:
fn = DEF_FN
if not cfg:
cfg = common.get_config()
with open(fn, "w") as fh:
_write_line('# General stuff', fh)
for (out_name, cfg_data) in CFG_MAKE.items():
section = cfg_data[0]
key = cfg_data[1]
value = cfg.get(section, key, auto_pw=False)
if value:
_write_env(out_name, value, fh)
_write_line("", fh)
_generate_ec2_env(fh, cfg)
_generate_nova_env(fh, cfg)
_generate_os_env(fh, cfg)
def load_local_rc(fn=None, cfg=None):
if not fn:
fn = DEF_FN
if not cfg:
cfg = common.get_config()
am_set = 0
with open(fn, "r") as fh:
for line in fh:
m = EXP_PAT.search(line)
if m:
var = m.group(1).strip()
value = m.group(2).strip()
if not var in os.environ and var and value:
os.environ[var] = value
am_set += 1
return am_set

View File

@@ -18,6 +18,7 @@ import time
from devstack import cfg
from devstack import date
from devstack import env_rc
from devstack import exceptions as excp
from devstack import log as logging
from devstack import settings
@@ -31,8 +32,6 @@ from devstack.components import keystone
from devstack.progs import common
from utils import env_gen
LOG = logging.getLogger("devstack.progs.actions")
# This map controls which distro has
@@ -86,7 +85,7 @@ def _pre_run(action_name, root_dir, pkg_manager, config, component_order, instan
try:
if sh.isfile(rc_fn):
LOG.info("Attempting to load rc file at [%s] which has your environment settings." % (rc_fn))
am_loaded = env_gen.load_local_rc(rc_fn, config)
am_loaded = env_rc.load_local_rc(rc_fn, config)
loaded_env = True
LOG.info("Loaded [%s] settings from rc file [%s]" % (am_loaded, rc_fn))
except IOError:
@@ -281,7 +280,7 @@ def _instanciate_components(action_name, components, distro, pkg_manager, config
def _gen_localrc(config, fn):
LOG.info("Generating a file at [%s] that will contain your environment settings." % (fn))
env_gen.generate_local_rc(fn, config)
env_rc.generate_local_rc(fn, config)
def _run_components(action_name, component_order, components, distro, root_dir, program_args):

View File

@@ -18,8 +18,6 @@
import optparse
import os
import re
import subprocess
import sys
POSSIBLE_TOPDIR = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
@@ -27,135 +25,13 @@ POSSIBLE_TOPDIR = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
os.pardir))
sys.path.insert(0, POSSIBLE_TOPDIR)
from devstack import cfg
from devstack import shell as sh
from devstack import env_rc
from devstack import utils
from devstack import shell as sh
from devstack.progs import common
PROG_NAME = "Env. rc file generator"
PROG_NAME = "Env. file generator"
CFG_MAKE = {
'ADMIN_PASSWORD': ('passwords', 'horizon_keystone_admin'),
'MYSQL_PASSWORD': ('passwords', 'sql'),
'RABBIT_PASSWORD': ('passwords', 'rabbit'),
'SERVICE_TOKEN': ('passwords', 'service_token'),
'FLAT_INTERFACE': ('nova', 'flat_interface'),
'HOST_IP': ('host', 'ip'),
}
DEF_FN = 'localrc'
#various settings we will output
EC2_PORT = 8773
S3_PORT = 3333
#these are pretty useless
EC2_USER_ID = 42
EC2_ACCESS_KEY = 'demo'
#change if you adjust keystone
NOVA_PORT = 5000
NOVA_VERSION = '1.1'
NOVA_PROJECT = 'demo'
NOVA_REGION = 'RegionOne'
#change if you adjust keystone
OS_TENANT_NAME = 'demo'
OS_USERNAME = 'demo'
OS_AUTH_PORT = 5000
def write_env(name, value, fh):
str_value = str(value)
escaped_val = subprocess.list2cmdline([str_value])
if str_value != escaped_val:
fh.write("export %s=\"%s\"" % (name, escaped_val))
else:
fh.write("export %s=%s" % (name, str_value))
fh.write(os.linesep)
def generate_ec2_env(fh, cfg):
fh.write(os.linesep)
fh.write('# EC2 and/or S3 stuff')
fh.write(os.linesep)
ip = cfg.get('host', 'ip')
write_env('EC2_URL', 'http://%s:%s/services/Cloud' % (ip, EC2_PORT), fh)
write_env('S3_URL', 'http://%s:%s/services/Cloud' % (ip, S3_PORT), fh)
write_env('EC2_ACCESS_KEY', EC2_ACCESS_KEY, fh)
hkpw = cfg.get('passwords', 'horizon_keystone_admin', auto_pw=False)
if hkpw:
write_env('EC2_SECRET_KEY', hkpw, fh)
write_env('EC2_USER_ID', EC2_USER_ID, fh)
write_env('EC2_CERT', '~/cert.pem', fh)
def generate_nova_env(fh, cfg):
fh.write(os.linesep)
fh.write('# Nova stuff')
fh.write(os.linesep)
ip = cfg.get('host', 'ip')
hkpw = cfg.get('passwords', 'horizon_keystone_admin', auto_pw=False)
if hkpw:
write_env('NOVA_PASSWORD', hkpw, fh)
write_env('NOVA_URL', 'http://%s:%s/v2.0' % (ip, NOVA_PORT), fh)
write_env('NOVA_PROJECT_ID', NOVA_PROJECT, fh)
write_env('NOVA_REGION_NAME', NOVA_REGION, fh)
write_env('NOVA_VERSION', NOVA_VERSION, fh)
write_env('NOVA_CERT', '~/cacert.pem', fh)
def generate_os_env(fh, cfg):
fh.write(os.linesep)
fh.write('# Openstack stuff')
fh.write(os.linesep)
ip = cfg.get('host', 'ip')
hkpw = cfg.get('passwords', 'horizon_keystone_admin', auto_pw=False)
if hkpw:
write_env('OS_PASSWORD', hkpw, fh)
write_env('OS_TENANT_NAME', OS_TENANT_NAME, fh)
write_env('OS_USERNAME', OS_USERNAME, fh)
write_env('OS_AUTH_URL', 'http://%s:%s/v2.0' % (ip, OS_AUTH_PORT), fh)
def generate_local_rc(fn=None, cfg=None):
if not fn:
fn = DEF_FN
if not cfg:
cfg = common.get_config()
with open(fn, "w") as fh:
fh.write('# General stuff')
fh.write(os.linesep)
for (out_name, cfg_data) in CFG_MAKE.items():
section = cfg_data[0]
key = cfg_data[1]
value = cfg.get(section, key, auto_pw=False)
if value:
write_env(out_name, value, fh)
generate_ec2_env(fh, cfg)
generate_nova_env(fh, cfg)
generate_os_env(fh, cfg)
def load_local_rc(fn=None, cfg=None):
if not fn:
fn = DEF_FN
if not cfg:
cfg = common.get_config()
pattern = re.compile("^export (.*?)=(.*?)$", re.IGNORECASE)
am_set = 0
with open(fn, "r") as fh:
for line in fh:
m = pattern.search(line)
if m:
var = m.group(1).strip()
value = m.group(2).strip()
if not var in os.environ and var and value:
os.environ[var] = value
am_set += 1
return am_set
DEF_FN = 'openstackrc'
def main():
@@ -167,7 +43,7 @@ def main():
fn = options.filename
if not fn:
fn = DEF_FN
generate_local_rc(fn)
env_rc.generate_local_rc(fn)
print("Check file \"%s\" for your environment configuration." \
% (sh.abspth(fn)))
return 0

View File

@@ -1,170 +0,0 @@
#!/usr/bin/perl -w
use warnings;
use strict;
use FileHandle;
use Term::ANSIColor qw(:constants);
sub printinfo
{
print BOLD, BLUE, "INFO: "."", RESET;
println("@_");
}
sub printerror
{
print BOLD, RED, "ERROR: @_"."\n", RESET;
}
sub run
{
my ($prog, $die) = @_;
#printinfo("Runing command: $prog");
my $res = qx/$prog/;
my $ok = 0;
my $rc = $? >> 8;
if($rc == 0)
{
$ok = 1;
}
if($ok == 0 && $die == 1)
{
printerror("Failed running $prog");
exit(1);
}
$res = trim($res);
my $out = {};
$out->{'status'} = $rc;
$out->{'output'} = $res;
return $out;
}
sub println
{
my $arg = shift;
if(!defined($arg))
{
$arg = '';
}
return print($arg."\n");
}
sub trim
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
my $argc = scalar(@ARGV);
if($argc == 0)
{
println($0. " pkglist");
exit(1);
}
my $fn = $ARGV[0];
my $fh = new FileHandle($fn, "r") || die("Could not open $fn");;
my @lines = <$fh>;
$fh->close();
my @all = ();
my $ks = {};
for my $line (@lines)
{
$line = trim($line);
if(length($line) == 0)
{
next;
}
my @pieces = split /\s+/, $line;
for my $piece (@pieces)
{
$piece = trim($piece);
if(length($piece) == 0)
{
next;
}
if(defined($ks->{$piece}))
{
next;
}
push(@all, $piece);
$ks->{$piece} = 1;
}
}
@all = sort(@all);
printinfo("Finding info about packages:");
println(join(", ", @all)."");
my $info = {};
for my $pkg (@all)
{
printinfo("Finding information about $pkg");
my $cmd = "apt-cache showpkg $pkg";
my $out = run($cmd, 1)->{'output'};
my $version = undef;
if($out =~ /Versions:\s+([\S]+)\s+/msi)
{
$version = $1;
}
else
{
printerror("No version found for $pkg");
exit(1);
}
$cmd = "apt-cache depends $pkg";
$out = run($cmd, 1)->{'output'};
my @tmplines = split /\n|\r/, $out;
my @deps = ();
for my $aline (@tmplines)
{
if($aline =~ /\s+Depends:\s*(\S+)\s*/i)
{
my $dep = trim($1);
if(length($dep) > 0)
{
if($dep =~ /[<>]/)
{
#not sure why we get these...
next;
}
push(@deps, $dep);
}
}
}
my $d = {};
$d->{'deps'} = \@deps;
$d->{'version'} = $version;
$info->{$pkg} = $d;
}
for my $pkg (@all)
{
my $data = $info->{$pkg};
my $version = $data->{version};
print STDERR ("+Package name: $pkg\n");
print STDERR ("+Package version: $version\n");
my @deps = @{$data->{deps}};
@deps = sort(@deps);
my $tmpk = {};
print STDERR ("+Dependencies:\n");
for my $dep (@deps)
{
if(defined($tmpk->{$dep}))
{
next;
}
print STDERR ("\t"."$dep\n");
$tmpk->{$dep} = 1;
}
}
exit(0);