1. Albert Hopkins
  2. hemp

Commits

Albert Hopkins  committed 78cda37

New cloud metadata "format" includes the number of servers in the cloud and an optional description.

Changed command parser to accept a "description" file for "pack"

Implemented logic to error out if cloud file format is a more recent
revision than the running hemp can deal with.

  • Participants
  • Parent commits 31daf89
  • Branches default
  • Tags 0.4.4.1

Comments (0)

Files changed (1)

File hemp/hemp.py

View file
 
 import settings
 
-__version__ = '0.4.4'
+__version__ = '0.4.4.1'
 __revision__ = '$Revision$'
 
 DIRS = ('images',)
 FILES = ('fabfile.py', 'helpers.py', 'settings.py', 'custom.py')
 
 CLOUD_FILE_SIGNATURE = '\x08\x05\x13\x16'
-CLOUD_FILE_FORMAT = 1
+CLOUD_FILE_FORMAT = 2
 
 class InvalidCloudFile(Exception):
     """
     [shutil.copy(i, basedir) for i in files]
 
 
+def get_num_servers_for(path):
+    """Return the number of servers configured for a hemp project
+    <<path>> is the directory to the project (where fabfile.py resides)
+    """
+    import imp
+
+    file_o, filename, metadata = imp.find_module('settings', [path])
+    module = imp.load_module('settings', file_o, filename, metadata)
+
+    num_servers = getattr(module, 'NUM_SERVERS', 0)
+    roles = getattr(module, 'ROLES', {'bugus': 0})
+    roles_count = sum(roles.values())
+    return max(num_servers, roles_count)
+
 def init(args):
     """Initialize a hemp project"""
     basevm = args.basevm
     url = args.url or ''
     outfile = args.file or '%s.cloud' % name
     create_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
+    num_servers = get_num_servers_for(args.path)
+
+    if args.description:
+        try:
+            desc_file = open(args.description, 'r')
+        except (IOError, OSError) as error:
+            sys.stderr.write('Error opening descriptioin file:\n')
+            sys.stderr.write('%s\n' % error)
+            sys.exit(1)
+        description = desc_file.read()
+        desc_file.close()
 
     metadata = dict(
         version=CLOUD_FILE_FORMAT,
         author=author,
         create_time=create_time,
         url=url,
+        servers=num_servers,
+        description=description,
     )
 
     # open file for writing
         sys.stderr.write('%s\n' % error)
         sys.exit(1)
 
-    print 'FILE    : %s' % args.filename
-    print 'CREATED : %s' % metadata['create_time']
-    print 'AUTHOR  : %s' % metadata['author']
-    print 'NAME    : %s' % metadata['name']
+    print 'File    : %s' % os.path.basename(args.filename)
+    print 'Created : %s' % metadata['create_time']
+    print 'Author  : %s' % metadata['author']
+    print 'Name    : %s' % metadata['name']
     if metadata['url']:
         print 'URL     : %s' % metadata['url']
-    print 'ARCH    : %s' % metadata['machine']
+    print 'Arch    : %s' % metadata['machine']
+    print 'Servers : %s' % metadata.get('servers', '?')
+
+    if metadata.get('description', ''):
+        print
+        print metadata['description']
 
 def unpack(args):
     """Unpack a cloud"""
     project_name = metadata['name']
     path = os.path.join(outdir, project_name)
 
+    if metadata['version'] > CLOUD_FILE_SIGNATURE:
+        sys.stderr.write('The cloud file contains a cloud in a format'
+            'that this\n')
+        sys.stderr.write('version of hemp does not understand.\n')
+        sys.stderr.write('Please upgrade to a newer version of hemp.\n')
+        sys.exit(2)
+
     try:
         os.mkdir(path)
     except (IOError, OSError) as error:
             type=str,
             default=None,
     )
+    parser_pack.add_argument('-d', '--description',
+        help='Filename providing a description for the cloud',
+        type=str,
+        default=None,
+    )
     parser_pack.set_defaults(func=pack)
 
     parser_info = subparsers.add_parser('info',