pulumi-command icon indicating copy to clipboard operation
pulumi-command copied to clipboard

Consider contents of file for changes in CopyFile

Open gingerwizard opened this issue 3 years ago • 3 comments

  • Vote on this issue by adding a 👍 reaction
  • If you want to implement this feature, comment to let us know (we'll work with you on design, scheduling, etc.)

Issue details

Currently if the contents of a file changes passed by CopyFile no change is detected. Ideally this would prompt an update. Furthermore, if a command e.g. script execution, depends on this file these should be considered dependent.

gingerwizard avatar Jan 05 '22 23:01 gingerwizard

One thing you can do is use triggers and pass in a hash of the file contents as a way to do this.

leezen avatar Jan 08 '22 01:01 leezen

I was having a similar problem. Here is how I implemented my pulumi script to replace the remote file when the local file changes.

import pulumi
from pulumi_aws import ec2
from pulumi_command import remote


def main():
    # Ubuntu Server 20.04 LTS (HVM), SSD Volume Type for us-east-2
    ami_id = "ami-0fb653ca2d3203ac1"
    key_pair = ec2.get_key_pair(key_pair_id="XXXX")

    rsw_server = ec2.Instance(
        f"rstudio-workbench-server",
        instance_type="t3.medium",
        ami=ami_id,                 
        key_name=key_pair.key_name
    )

    connection = remote.ConnectionArgs(
        host=rsw_server.public_dns, 
        user="ubuntu", 
        private_key="XXXX"
    )

    with open("server-side-justfile", mode="r") as f:
        justfile_hash = pulumi.Output.concat(str(hash(f.read())))

    command_copy_justfile = remote.CopyFile(
        f"server-copy-justfile-",  
        local_path="server-side-justfile", 
        remote_path='justfile', 
        connection=connection, 
        opts=pulumi.ResourceOptions(depends_on=[rsw_server]),
        triggers=[justfile_hash]
    )

main()

SamEdwardes avatar Jun 23 '22 19:06 SamEdwardes

Here's my version in TS

import { BinaryLike, createHash } from "node:crypto";

export const revHash = (data: BinaryLike) =>
  createHash("sha256").update(data)
    .digest(
      "hex",
    );


const composeFileRevisionHash = revHash(
  stripComments(readFileSync("../docker-compose.yaml", { encoding: "utf-8" })),
);


const uploadCompose = new remote.CopyFile("Upload-Docker-Compose", {
    connection,
    localPath: "../docker-compose.yaml",
    remotePath: "/home/init/docker-compose.yaml",
    triggers: [composeFileRevisionHash],
  },
  { dependsOn: [createDirectories] },
);


// not necessarily needed, though I sometimes add or remove notes which shouldn't trigger an update
// inspired by: https://stackoverflow.com/a/59094308/3484824 && https://stackoverflow.com/a/36850576/3484824
export function stripComments(str: string) {
  return str
    .replace(
      /(\/\*[\wа-я\'\s\r\n\*]*\*\/)|(#.*)|(\/\/[\wа-я\s\'\;]*)|(\<![\-\-\s\wа-я\>\/]*\>)/g,
      "",
    )
    .trim()
    .split("\n")
    .filter((line) => line.trim() !== "")
    .join("\n");
}

CanRau avatar Jun 23 '22 19:06 CanRau