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

Upload image #63

Merged
merged 2 commits into from
Feb 11, 2020
Merged

Upload image #63

merged 2 commits into from
Feb 11, 2020

Conversation

skeiffer
Copy link
Contributor

@skeiffer skeiffer commented Feb 11, 2020

Added method to upload images to a GNS3 compute server. Primary use case is to facilitate the upload of bootstrap iso and img files.

Example of use (snip from ansible module):

def md5_for_file(file_name, block_size=2**20):
    """
    Gets MD5 of file
    """
    with open(file_name, "rb") as f:
        md5 = hashlib.md5()
        while True:
            data = f.read(block_size)
            if not data:
                break
            md5.update(data)
        return md5.hexdigest()

def check_existing_file(file_path, server, emulator):
    """
    Checks server for existing file and validates hash
    """

    # Get file hash
    md5sum = md5_for_file(file_path)

    # Get existing images
    existing_images = server.get_compute_images(emulator)

    # Check if file exists and hash matches
    file_name = os.path.basename(file_path)
    return any(i for i in existing_images if i["filename"] == file_name and i["md5sum"] == md5sum)

def main():
    module = AnsibleModule(
        argument_spec=dict(
            url=dict(type="str", required=True),
            port=dict(type="int", default=3080),
            user=dict(type="str", default=None),
            password=dict(type="str", default=None, no_log=True),
            emulator=dict(type="str", default="qemu"),
            file=dict(type="str", required=True)
        )
    )
    if not HAS_GNS3FY:
        module.fail_json(msg=missing_required_lib("gns3fy"), exception=GNS3FY_IMP_ERR)
    result = dict(changed=False, local_compute=None, version=None)
    server_url = module.params["url"]
    server_port = module.params["port"]
    server_user = module.params["user"]
    server_password = module.params["password"]
    emulator = module.params["emulator"]
    file_path = module.params["file"]

    server = Gns3Connector(
        url=f"{server_url}:{server_port}", user=server_user, cred=server_password
    )

    # Validate file exists
    if not os.path.exists(file_path):
        module.fail_json(msg=f"File not found: {file_path}")

    result = dict(changed=False)

    # Check if file exists and hash matches
    existing_file = check_existing_file(file_path, server, emulator)
    if existing_file:
        module.exit_json(**result)

    result["changed"] = True

    if module.check_mode:
        module.exit_json(**result)

    # Upload file
    server.upload_compute_image(emulator, file_path)

    # Look for file post upload
    file_exists_post_upload = check_existing_file(file_path, server, emulator)
    if not file_exists_post_upload:
        module.fail_json(msg="File could not be verified after upload!")

    module.exit_json(**result)  

@skeiffer skeiffer requested a review from davidban77 February 11, 2020 03:04
@davidban77 davidban77 merged commit 56422e7 into davidban77:develop Feb 11, 2020
@davidban77
Copy link
Owner

Thanks again for the PR. Will submit a new release soon, in the meantime you can use the develop branch for your work.

davidban77 added a commit that referenced this pull request May 8, 2020
* Adding extra tag

* Updating pydantic and fixing changelog.md

* Refactoring verify_connector_and_id

* Fixing formatting

* Upload image (#63)

* minimal GNS3 version

* Added compute image upload method.

Co-authored-by: louis-oui <47607835+louis-oui@users.noreply.github.com>

* Upgrading to poetry v1.0 (#67)

* Enhancement py38 support (#68)

* Updating and fixing envlist in tox

* Adding tox-conda requirements and changing image on circleci

* Fixing step to install poetry

* Adding python 3.8 in classifiers

* Enhancement docker dev environment (#69)

* Adding devcontainer for vscode standardized testing

* Fixing the project close method update and wrap docstring

* fixing tests and adding some docs (#72)

Co-authored-by: Scott Keiffer <eskeiffer@gmail.com>
Co-authored-by: louis-oui <47607835+louis-oui@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants