```import * as pulumi from "@pulumi/pulumi"; impor...
# typescript
b
Copy code
import * as pulumi from "@pulumi/pulumi";
import * as equinix_metal from "@pulumi/equinix-metal";
import * as fs from "fs";

const userData = fs.readFileSync('bs.sh','utf8');
const projectId = "xxx";
const testProjectSshKey = new equinix_metal.ProjectSshKey("testProjectSshKey", {
    publicKey: "ssh-rsa ",
    projectId: projectId,
});

const dsbasenode = new equinix_metal.Device("ds-base-node",{
  hostname: "ds-base-node",
  plan: "c3.small.x86",
  metro: "sv",
  operatingSystem: "ubuntu_20_04",
  billingCycle: "hourly",
  projectSshKeyIds: [testProjectSshKey.id],
  projectId: projectId,
  userData: fs.readFileSync('./basenode.sh'),
});
// Export the name of the project
export const dsbasenodeIP= dsbasenode.accessPublicIpv4;
l
How is userData defined in Device's args interface? Is it an
Output<string>
? If you change it to an
Input<string>
it should work.
In almost all cases,
Output
should not be used for passing values around: always
Input
around.
b
@little-cartoon-10569 can you please provide example of it ?
l
Where is Device's args interface defined? Is it in your code?
I see it in the registry: https://www.pulumi.com/registry/packages/equinix-metal/api-docs/device/#userdata_nodejs Since this isn't your code, this isn't the problem.
Ah I see your problem. You're not providing an encoding to
readFileSync()
, so it's returning a Buffer. Provide an encoding and the problem will go away.
So change this:
userData: fs.readFileSync('./basenode.sh'),
to this:
userData: fs.readFileSync('./basenode.sh', 'utf8'),
b
Copy code
#!/bin/sh

sudo apt-get install -y ansible

sudo apt install python3.8

wget -qO - terraform.gpg <https://apt.releases.hashicorp.com/gpg> | sudo gpg --dearmor -o /usr/share/keyrings/terraform-archive-keyring.gpg

sudo echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/terraform-archive-keyring.gpg] <https://apt.releases.hashicorp.com> $(lsb_release -cs) main" > /etc/apt/sources.list.d/terraform.list

sudo apt install terraform
So above is my shell script
l
Shell script isn't important. Pulumi isn't even called in there.
Oh is that your userData?
b
Nope I did changes as per @stocky-restaurant-98004 it worked but it’s just read it
1
Copy code
import * as pulumi from "@pulumi/pulumi";
import * as equinix_metal from "@pulumi/equinix-metal";
import * as fs from "fs";
import { readFileSync } from "fs";

const userData = readFileSync("bs.sh", {
  encoding: "utf-8",
});

export { userData };

const projectId = "XXX";
const testProjectSshKey = new equinix_metal.ProjectSshKey("testProjectSshKey", {
    publicKey: "ssh-rsa ",
    projectId: projectId,
});

const dsbasenode = new equinix_metal.Device("ds-base-node",{
  hostname: "ds-base-node",
  plan: "c3.small.x86",
  metro: "sv",
  operatingSystem: "ubuntu_20_04",
  billingCycle: "hourly",
  projectSshKeyIds: [testProjectSshKey.id],
  projectId: projectId,
});
// Export the name of the project
export const dsbasenodeIP= dsbasenode.accessPublicIpv4;
Copy code
Resources:
    3 unchanged

Do you want to perform this update? yes
Updating (stage):
     Type                 Name              Status     
     pulumi:pulumi:Stack  metal-auto-stage             
 
Outputs:
    dsbasenodeIP: "139.178.70.225"
    userData    : "#!/bin/sh\n\nsudo apt-get install -y ansible\n\nsudo apt install python3.8\n\nwget -qO - terraform.gpg <https://apt.releases.hashicorp.com/gpg> | sudo gpg --dearmor -o /usr/share/keyrings/terraform-archive-keyring.gpg\n\nsudo echo \"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/terraform-archive-keyring.gpg] <https://apt.releases.hashicorp.com> $(lsb_release -cs) main\" > /etc/apt/sources.list.d/terraform.list\n\nsudo apt install terraform\n\n\n"
Server created , file was read successfull but content is still unread ( Shell didn’t execute )
l
You've removed userData from Device? Why?
Put that back and it'll work.
b
Copy code
Do you want to perform this update? yes
Updating (stage):
     Type                                  Name               Status      Info
 +   pulumi:pulumi:Stack                   metal-auto-stage   created     13 messages
 +   ├─ equinix-metal:index:ProjectSshKey  testProjectSshKey  created     
 +   └─ equinix-metal:index:Device         ds-base-node       created     
 
Diagnostics:
  pulumi:pulumi:Stack (metal-auto-stage):
    2022/09/27 13:54:37 [DEBUG] POST <https://api.equinix.com/metal/v1/projects/e981d829-e0ac-463a-ad82-57bae2063c61/ssh-keys>
    2022/09/27 13:54:38 [DEBUG] GET <https://api.equinix.com/metal/v1/ssh-keys/4ffc7bb8-eb79-48fc-a268-755d4c9dd4ad>
    2022/09/27 13:54:40 [DEBUG] POST <https://api.equinix.com/metal/v1/projects/e981d829-e0ac-463a-ad82-57bae2063c61/devices>
    2022/09/27 13:54:51 [DEBUG] GET <https://api.equinix.com/metal/v1/devices/1daef2a9-2b19-4ed1-be34-b255a901eabd?include=project%2Cfacility>
    2022/09/27 13:54:56 [DEBUG] GET <https://api.equinix.com/metal/v1/devices/1daef2a9-2b19-4ed1-be34-b255a901eabd?include=project%2Cfacility>
    2022/09/27 13:55:03 [DEBUG] GET <https://api.equinix.com/metal/v1/devices/1daef2a9-2b19-4ed1-be34-b255a901eabd?include=project%2Cfacility>
    2022/09/27 13:55:14 [DEBUG] GET <https://api.equinix.com/metal/v1/devices/1daef2a9-2b19-4ed1-be34-b255a901eabd?include=project%2Cfacility>
    2022/09/27 13:55:26 [DEBUG] GET <https://api.equinix.com/metal/v1/devices/1daef2a9-2b19-4ed1-be34-b255a901eabd?include=project%2Cfacility>
    2022/09/27 13:55:37 [DEBUG] GET <https://api.equinix.com/metal/v1/devices/1daef2a9-2b19-4ed1-be34-b255a901eabd?include=project%2Cfacility>
    2022/09/27 13:55:49 [DEBUG] GET <https://api.equinix.com/metal/v1/devices/1daef2a9-2b19-4ed1-be34-b255a901eabd?include=project%2Cfacility>
    2022/09/27 13:56:00 [DEBUG] GET <https://api.equinix.com/metal/v1/devices/1daef2a9-2b19-4ed1-be34-b255a901eabd?include=project%2Cfacility>
    2022/09/27 13:56:12 [DEBUG] GET <https://api.equinix.com/metal/v1/devices/1daef2a9-2b19-4ed1-be34-b255a901eabd?include=project%2Cfacility>
    2022/09/27 13:56:13 [DEBUG] GET <https://api.equinix.com/metal/v1/devices/1daef2a9-2b19-4ed1-be34-b255a901eabd?include=project%2Cmetro%2Cfacility>
 
Outputs:
    dsbasenodeIP: "139.178.70.225"
    userData    : "#!/bin/sh\n\nsudo apt-get install -y ansible\n\nsudo apt install python3.8\n\nwget -qO - terraform.gpg <https://apt.releases.hashicorp.com/gpg> | sudo gpg --dearmor -o /usr/share/keyrings/terraform-archive-keyring.gpg\n\nsudo echo \"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/terraform-archive-keyring.gpg] <https://apt.releases.hashicorp.com> $(lsb_release -cs) main\" > /etc/apt/sources.list.d/terraform.list\n\nsudo apt install terraform\n\n\n"

Resources:
    + 3 created
root@ds-base-node:~# ls
snap
root@ds-base-node:~#
As I said it runs content of file but doesn’t execute it
Copy code
root@ds-base-node:~# ls
snap
root@ds-base-node:~# ls
snap
root@ds-base-node:~# terraform --version

Command 'terraform' not found, but can be installed with:
Copy code
#!/bin/env node
Even after doing above ☝️
s
Did you check the userdata logs? What happens if you just do
touch ~/userdata_works
in your script?
Check:
/var/lib/cloud/instance/user-data.txt
/var/log/cloud-init.log and /var/log/cloud-init-output.log