def test(ctx):
    Docker.build(cli,
        dockerfile='Dockerfile.dev',
        tag="{0}-dev".format(flow.get_branch_container_name())
    )

    Docker.run(cli,
        tag="{0}-dev".format(flow.get_branch_container_name()),
        command='glide install',
        volumes=[
            "{0}:/go/src/github.com/vjftw/homomorphic-encryption/backend".format(os.getcwd())
        ],
        working_dir="/go/src/github.com/vjftw/homomorphic-encryption/backend",
        environment={}
    )
    Docker.run(cli,
        tag="{0}-dev".format(flow.get_branch_container_name()),
        command='/bin/sh -c "go test -v $(glide novendor)"',
        volumes=[
            "{0}:/go/src/github.com/vjftw/homomorphic-encryption/backend".format(os.getcwd())
        ],
        working_dir="/go/src/github.com/vjftw/homomorphic-encryption/backend",
        environment={}
    )
    pass
def appengine_deploy(ctx):
    """
    appcfg.py
    """
    # Docker.clean(cli, ["appengine/client-dist"])
    # print("\nBuilding Client\n")
    # os.chdir("client")
    # ctx.run("invoke build_prod")
    #
    # print("\nCopying built Client\n")
    # shutil.copytree("dist", "../appengine/client-dist")

    print("\nCopying Credentials file\n")
    Docker.clean(cli, ["appengine/gae-creds"])

    # os.chdir("../appengine")
    os.makedirs("{0}/appengine/gae-creds".format(os.getcwd()))
    shutil.copy(os.getenv("GAEServiceCredentialsFile"), "appengine/gae-creds/GAEServiceCredentials.json")
    with open("appengine/gae-creds/GAEServiceCredentials.json") as json_data:
        creds = json.loads(json_data.read())

    print("\nUploading to Google AppEngine\n")
    Docker.build(cli,
        dockerfile="appengine/Dockerfile.appengine",
        tag="vjftw/gae"
    )
    print(creds['client_email'])
    cmd1 = "gcloud auth activate-service-account {0} --key-file /app/appengine/gae-creds/GAEServiceCredentials.json".format(creds['client_email'])
    cmd2 = "gcloud config set account {0}".format(creds['client_email'])
    cmd3 = "cd appengine && appcfg.py -A homomorphic-encryption -V v1 update ."

    Docker.run(cli,
        tag="vjftw/gae",
        command='/bin/sh -c "{0} && {1} && {2}"'.format(cmd1, cmd2, cmd3),
        volumes=[
            "{0}:/app".format(os.getcwd())
        ],
        working_dir="/app"
    )

    # ctx.run("appcfg.py -A homomorphic-encryption -V v1 update ./")
    pass
def pre_clean(ctx):
    Docker.clean(cli,
        ['node_modules', 'doc', 'typings', 'coverage', 'dist']
    )
def build_prod(ctx):
    print("# Building!")
    Docker.build(cli,
        dockerfile='Dockerfile.dev',
        tag="{0}-dev".format(flow.get_branch_container_name())
    )

    print("Starting Development container")
    prod_api_url = 'homomorphic-encryption.appspot.com'
    prod_backend_url = 'homomorphic-encryption.appspot.com'

    container = cli.create_container(
        image="{0}-dev".format(flow.get_branch_container_name()),
        volumes=[
            '{0}:/app'.format(os.getcwd())
        ],
        host_config=cli.create_host_config(binds=[
            '{0}:/app'.format(os.getcwd())
        ]),
        environment={
            'CLIENT_API_ADDRESS': prod_api_url,
            'CLIENT_BACKEND_ADDRESS': prod_backend_url
        }
    )

    response = cli.start(container=container.get('Id'))

    Docker.execute(cli, container.get('Id'), "node --version")
    Docker.execute(cli, container.get('Id'), "npm --version")

    Docker.execute(cli, container.get('Id'), "npm install")
    Docker.execute(cli, container.get('Id'), "npm run postinstall")

    Docker.execute(cli, container.get('Id'), "npm run build:prod")

    cli.stop(container.get('Id'))
    cli.remove_container(container.get('Id'))

    print("# Building Production container!")
    Docker.build(cli, "Dockerfile.app", flow.get_build_container_name())
def test(ctx):
    print("# Testing!")
    Docker.build(cli,
        dockerfile='Dockerfile.dev',
        tag="{0}-dev".format(flow.get_branch_container_name())
    )

    print("Starting Development container")
    container = cli.create_container(
        image="{0}-dev".format(flow.get_branch_container_name()),
        volumes=[
            '{0}:/app'.format(os.getcwd())
        ],
        host_config=cli.create_host_config(binds=[
            '{0}:/app'.format(os.getcwd())
        ])
    )
    response = cli.start(container=container.get('Id'))
    # print(response)

    Docker.execute(cli, container.get('Id'), "node --version")
    Docker.execute(cli, container.get('Id'), "npm --version")
    Docker.execute(cli, container.get('Id'), "npm install")
    Docker.execute(cli, container.get('Id'), "npm run postinstall")
    Docker.execute(cli, container.get('Id'), "npm run lint")
    Docker.execute(cli, container.get('Id'), "npm run test")

    cli.stop(container.get('Id'))
    cli.remove_container(container.get('Id'))