How to add default customised script for every VM ?

asked 2017-04-26 01:16:01 -0600

Mohan gravatar image

I have some script to be run for every VM launching in my cloud. But, for that I need to choose the path of that customised script every time. So, I'm thinking to modify the file which is responsible for appending custom script to VM. I guess the file path is: /usr/share/openstack-dashboard/openstack_dashboard/dashboards/project/instances/workflows/create_instance.py

If it's a wrong path please update me with proper path to be modified. Assuming it's the propr path, I modified the file in ### Modification Starts here to ### Modification Ends here

Please suggest me how to properly make the task.

class CustomizeAction(workflows.Action):
class Meta(object):
    name = _("Post-Creation")
    help_text_template = ("project/instances/"
                          "_launch_customize_help.html")

    source_choices = [('', _('Select Script Source')),
                  ('raw', _('Direct Input')),
                  ('file', _('File'))]

    attributes = {'class': 'switchable', 'data-slug': 'scriptsource'}
    script_source = forms.ChoiceField(
        label=_('Customization Script Source'),
        choices=source_choices,
        widget=forms.ThemableSelectWidget(attrs=attributes),
        required=False)

    script_help = _("A script or set of commands to be executed after the "
                    "instance has been built (max 16kb).")

    script_upload = forms.FileField(
        label=_('Script File'),
        help_text=script_help,
        widget=forms.FileInput(attrs={
            'class': 'switched',
            'data-switch-on': 'scriptsource',
            'data-scriptsource-file': _('Script File')}),
        required=False)

    script_data = forms.CharField(
        label=_('Script Data'),
        help_text=script_help,
        widget=forms.widgets.Textarea(attrs={
            'class': 'switched',
            'data-switch-on': 'scriptsource',
            'data-scriptsource-raw': _('Script Data')}),
        required=False)

    def __init__(self, *args):
        super(CustomizeAction, self).__init__(*args)

    def clean(self):
        cleaned = super(CustomizeAction, self).clean()

        files = self.request.FILES
        script = self.clean_uploaded_files('script', files)

    ###
    ### Modification starts here
    compulsory_script_filename = '/root/customisation/aFile.sh'
    with open(compulsory_script_filename, 'r') as content_file:
    content = content_file.read()

    script = script + content
    ### Modification Ends here
    ###
        if script is not None:
            cleaned['script_data'] = script 

        return cleaned

    def clean_uploaded_files(self, prefix, files):
        upload_str = prefix + "_upload"

        has_upload = upload_str in files
        if has_upload:
            upload_file = files[upload_str]
            log_script_name = upload_file.name
            LOG.info('got upload %s' % log_script_name)

            if upload_file._size > 16 * units.Ki:  # 16kb
                msg = _('File exceeds maximum size (16kb)')
                raise forms.ValidationError(msg)
            else:
                script = upload_file.read() ### Reading done here
                if script != "":
                    try:
                        normalize_newlines(script)
                    except Exception as e:
                        msg = _('There was a problem parsing the'
                                ' %(prefix)s: %(error)s')
                        msg = msg % {'prefix': prefix,
                                     'error': six.text_type(e)}
                        raise forms.ValidationError(msg)
                return script
        else:
            return None


class PostCreationStep(workflows.Step):
    action_class = CustomizeAction
    contributes = ("script_data",)
edit retag flag offensive close merge delete