Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate an init file that can be used with exported images #477

Merged
merged 1 commit into from
Mar 16, 2017

Conversation

gbenhaim
Copy link
Member

The generated init file will represent the env and will contain
relative paths to the exported images.

Signed-off-by: gbenhaim [email protected]

closes #473

@gbenhaim gbenhaim added this to the Demo Tool milestone Mar 14, 2017
@gbenhaim gbenhaim requested a review from nvgoldin March 14, 2017 17:09
Copy link
Contributor

@nvgoldin nvgoldin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

About the overall approach - I'm still not sure if "positive" checking wouldn't have made it easier:

  1. You have a list of "filters", which can even be callables(like re.match partials).
  2. You traverse through the graph, always keeping your current 'path'. For each node you visit you check if any of the callables returns True. If it does you delete it and never visit his sons(so BFS would make sense here).
    So when you finish travelling, you get only the nodes which passed(or should I say did not pass) the filters.

But, you'd still have to decide how to construct "paths" when you meet lists versus dicts... so perhaps its not worth it.

@@ -27,6 +27,7 @@
create an alternative implementation of the provisioning details for the VM,
for example, using a remote libvirt instance or similar.
"""
from copy import deepcopy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have our "specific" implementation of deepcopy in 'utils.py', I guess here its safe? (it was added to avoid the cross-references issue iirc)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "cross-references" ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the discussion here: #346 (review)

Not 100% if its relevant, just checking.

@@ -364,6 +365,10 @@ def metadata(self):
def disks(self):
return self._spec['disks'][:]

@property
def spec(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes the new spec somewhat "read only", no? was that the intention?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.
Any change to the spec will effect the structure of the env. IMO changes to the env should be done with specific object's methods (in this case the 'vm' object), and not directly to its data.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

lago/utils.py Outdated
all the metadata dicts from all domains disks will be removed.

Args:
spec (dict): spec file to remove keys from
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its a dict(not a file) :) as you wrote..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

lago/utils.py Outdated
paths (list): list of paths to the keys that should be removed

Returns:
None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add 'raises' here(look elsewhere in the file the exact syntax so it would format sphinixily nicely)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

lago/utils.py Outdated
elif len(path) == 1:
key = path.pop()
if not isinstance(spec, dict):
raise LagoUserException(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would print a more detailed message here, that hints what to do or what the user did wrong.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

lago/utils.py Outdated
'does not exist'.format('/'.join(path), current)
)
except TypeError:
raise LagoUserException(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when is this condition met and why? and how is it different from line 726?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example for each exception (by the order I catch them):

You have tried to remove the following key - "template".
Keys can not be removed from type <type 'list'>
Please verify that path - "domains/*/disks/template" is valid
Glob char * should refer only to dict or list, not to <type 'int'>
Please fix path - "domains/*/memory/*/some_key"
Malformed path "domains/*/gpu/*", key "gpu" does not exist
Malformed path "domains/*/disks/root/vda", can not get by key from type <type 'list'>

for _, domain in spec['domains'].viewitems():
for disk in domain['disks']:
if disk['type'] == 'template':
disk['template_type'] = 'qcow2'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is the right place for this logic. Should probably be a different method.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is for generating an init file that can be used with the exported images 'out of the box'.
For general spec you should use get_env_spec

lago/virt.py Outdated
'$LAGO_INITFILE_PATH', os.path.basename(disk['path'])
)

with open(os.path.join(dst_dir, 'LagoInitFile'), 'wt') as f:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dumping filename should be configuable, moreover it should drop it to stdout by default(like ansible_hosts cmd), so you could do something like:

lago dump-init >> my-init-el73-2011

Not sure if its an issue but if the file already exists(LagoInitFile) it will override it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is called only when exporting images, so it makes sense to write directly to a file.
I agree that the filename should be configurable.

@@ -961,7 +961,8 @@ def _create_disks(
'dev': disk['dev'],
'format': disk['format'],
'metadata': metadata,
'type': disk['type']
'type': disk['type'],
'name': disk['name']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

lago/utils.py Outdated
spec.pop(key, None)
else:
current = path[0]
if current == '*':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably better to add this as a default to the function:

def .... (..., sep='*')

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

lago/utils.py Outdated
"""

def remove_key(path, spec):
if len(path) == 0:
return
elif len(path) == 1:
key = path.pop()
if not isinstance(spec, dict):
if not isinstance(spec, collections.Mapping):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. This should work for OrderedDicts also?

lago/utils.py Outdated
format(key, spec, type(spec))
'You have tried to remove the following key - "{key}".\n'
'Keys can not be removed from type {spec_type}\n'
'Please verify that path - "{{path}}" is valid'.format(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

@gbenhaim
Copy link
Member Author

ci merge please

The generated init file will represent the env and will contain
relative paths to the exported images.

Signed-off-by: gbenhaim <[email protected]>
@gbenhaim
Copy link
Member Author

ci test please

@gbenhaim
Copy link
Member Author

ci merge please

@lago-bot lago-bot merged commit 0ea1688 into master Mar 16, 2017
@lago-bot lago-bot deleted the export_init_file branch March 16, 2017 12:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Auto generate the init file of exported env
3 participants