Skip to content

Commit

Permalink
Implement automation (#2)
Browse files Browse the repository at this point in the history
* added further automation for the script

* added macvm to the list of available options
  • Loading branch information
cjlapao authored Oct 11, 2023
1 parent 3e2422c commit fe4d5b3
Show file tree
Hide file tree
Showing 8 changed files with 530 additions and 64 deletions.
151 changes: 143 additions & 8 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ resource "aws_key_pair" "generated_key" {
resource "aws_ec2_host" "mac" {
count = var.machines_count

instance_type = var.use_intel ? "mac1.metal" : "mac2.metal"
instance_type = var.use_intel ? "mac1.metal" : var.use_m2_pro ? "mac2-m2pro.metal" : "mac2.metal"
availability_zone = data.aws_availability_zones.available.names[var.aws_availability_zone_index]
host_recovery = "off"
auto_placement = "on"
Expand Down Expand Up @@ -65,6 +65,22 @@ resource "aws_security_group" "ssh" {
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
description = "SSH from VPC"
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
description = "VNC from VPC"
from_port = 5900
to_port = 5900
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

egress {
from_port = 0
to_port = 0
Expand Down Expand Up @@ -94,13 +110,13 @@ resource "aws_instance" "mac" {
}
}

resource "time_sleep" "wait_60_seconds" {
resource "time_sleep" "wait_120_seconds" {
depends_on = [aws_instance.mac]

create_duration = "30s"
create_duration = "120s"
}

resource "terraform_data" "mac" {
resource "terraform_data" "init_script" {
count = var.machines_count

triggers_replace = {
Expand All @@ -114,19 +130,138 @@ resource "terraform_data" "mac" {
host = aws_instance.mac[count.index].public_ip
}

provisioner "remote-exec" {
script = "./scripts/init.sh"
}

depends_on = [
time_sleep.wait_120_seconds
]
}

resource "terraform_data" "install_parallels_desktop" {
count = var.install_parallels_desktop ? var.machines_count : 0

triggers_replace = {
file_hash = md5(file("./scripts/install-parallels-desktop.sh"))
}

connection {
type = "ssh"
user = "ec2-user"
private_key = tls_private_key.mac.private_key_pem
host = aws_instance.mac[count.index].public_ip
}

provisioner "remote-exec" {
inline = [
"rm ~/export.sh",
"touch ~/export.sh & chmod +x ~/export.sh",
"if [ -f ~/export.sh ]; then rm -f ~/export.sh; fi",
"echo \"export PARALLELS_KEY=${var.parallels_key}\" >> ~/export.sh",
"echo \"export PARALLELS_USER_EMAIL=${var.parallels_user_email}\" >> ~/export.sh",
"echo \"export PARALLELS_USER_PASSWORD=${var.parallels_user_password}\" >> ~/export.sh",
]
}

provisioner "remote-exec" {
script = "./scripts/init.sh"
script = "./scripts/install-parallels-desktop.sh"
}

depends_on = [
terraform_data.init_script
]
}

resource "terraform_data" "vnc_enable" {
count = var.enable_vnc ? var.machines_count : 0

connection {
type = "ssh"
user = "ec2-user"
private_key = tls_private_key.mac.private_key_pem
host = aws_instance.mac[count.index].public_ip
}

provisioner "remote-exec" {
inline = [
"if [ -f ~/export.sh ]; then rm -f ~/export.sh; fi",
"echo \"export VNC_USER_PASSWORD=${var.vnc_user_password}\" >> ~/export.sh",
]
}

provisioner "remote-exec" {
script = "./scripts/enable-vnc.sh"
}

depends_on = [time_sleep.wait_60_seconds]
depends_on = [
terraform_data.init_script
]
}

resource "terraform_data" "ubuntu" {
count = var.install_parallels_desktop ? var.machines_count : 0

triggers_replace = [
var.ubuntu_machines_count
]

connection {
type = "ssh"
user = "ec2-user"
private_key = tls_private_key.mac.private_key_pem
host = aws_instance.mac[count.index].public_ip
}

provisioner "remote-exec" {
inline = [
"if [ -f ~/export.sh ]; then rm -f ~/export.sh; fi",
"echo \"export MACHINE_COUNT=${var.ubuntu_machines_count}\" >> ~/export.sh",
"echo \"export MACHINE_NAME=${var.ubuntu_machine_name != "" ? "${replace(var.ubuntu_machine_name, " ", "\\ ")}" : "Ubuntu"}\" >> ~/export.sh",
"echo \"export UBUNTU_VERSION=${var.ubuntu_machine_os_version}\" >> ~/export.sh",
"echo \"export CPU_COUNT=${var.ubuntu_machine_cpu_count}\" >> ~/export.sh",
"echo \"export MEM_SIZE=${var.ubuntu_machine_mem_size}\" >> ~/export.sh",
"echo \"export DISK_SIZE=${var.ubuntu_machine_disk_size}\" >> ~/export.sh",
]
}

provisioner "remote-exec" {
script = "./scripts/create-ubuntu-vm.sh"
}

depends_on = [
terraform_data.install_parallels_desktop
]
}

resource "terraform_data" "macos" {
count = var.install_parallels_desktop ? var.machines_count : 0

triggers_replace = [
var.macos_machines_count
]

connection {
type = "ssh"
user = "ec2-user"
private_key = tls_private_key.mac.private_key_pem
host = aws_instance.mac[count.index].public_ip
}

provisioner "remote-exec" {
inline = [
"if [ -f ~/export.sh ]; then rm -f ~/export.sh; fi",
"echo \"export MACHINE_COUNT=${var.macos_machines_count}\" >> ~/export.sh",
"echo \"export MACHINE_NAME=${var.macos_machine_name != "" ? "${replace(var.macos_machine_name, " ", "\\ ")}" : "macOS"}\" >> ~/export.sh",
"echo \"export IPSW_URL=${var.macos_ipsw_url}\" >> ~/export.sh",
"echo \"export IPSW_CHECKSUM=${var.macos_ipsw_checksum}\" >> ~/export.sh",
]
}

provisioner "remote-exec" {
script = "./scripts/create-macos-vm.sh"
}

depends_on = [
terraform_data.install_parallels_desktop
]
}

10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,17 @@ For running the terraform script, you should:
machines_count = 1 // How many machines do you want to deploy
```


__Attention:__ At the moment of writing, only the US region has the new ARM Mac hardware and even there not all availability zones have them, check [here](https://aws.amazon.com/ec2/instance-types/mac/) before you choose.

by default the script will try to get the latest AMI available but you can set this manually by adding your own AMI ID or a specific version of the AMI, for example:

```json
mac_ami_id = "ami-09b2201fa15c4cb83"
```

__Attention:__ The AMI ID is different for each region and availability zone, so make sure you use the correct one.

* Run terraform init:

```bash
Expand Down
84 changes: 84 additions & 0 deletions scripts/create-macos-vm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/bin/bash

export PATH="/usr/local/bin:$PATH"
export USER_FOLDER=$(echo ~)
/Applications/Parallels\ Desktop.app/Contents/MacOS/Parallels\ Service service_restart

#check if export.sh exists
if [ -f ./export.sh ]; then
echo "export.sh file exists, setting the environment variables"
chmod +x ./export.sh
source ./export.sh
fi

if [ -z "$MACHINE_NAME" ]; then
MACHINE_NAME="MacOS"
fi

if [ -z "$MACHINE_COUNT" ]; then
MACHINE_COUNT=1
fi

if [ $MACHINE_COUNT -eq 0 ]; then
echo "MACHINE_COUNT is 0. Exiting..."
exit 0
fi

echo "Creating a directory for the code"
# Creating a directory for the code
if [ ! -d "$USER_FOLDER/code" ]; then
echo "Clone the packer-examples repo"
mkdir $USER_FOLDER/code
cd $USER_FOLDER/code

# clonning the packer-examples repo
git clone https://github.com/Parallels/packer-examples.git
fi

cd $USER_FOLDER/code/packer-examples/macos

# Loop over the number of machines specified by $MACHINE_COUNT
for ((i = 1; i <= $MACHINE_COUNT; i++)); do
echo "Checking if the virtual machine $MACHINE_NAME-$i already exists"
# Checking if the virtual machine already exists
/usr/local/bin/prlctl list $MACHINE_NAME-$i
prlctl_exit_code=$?
if [ $prlctl_exit_code -eq 0 ]; then
continue
fi

if [ -f ubuntu.pkrvars.hcl ]; then
rm macos.pkrvars.hcl
fi

echo "Initializing the packer project"
/opt/homebrew/bin/packer init .

echo "machine_name = \"$MACHINE_NAME-$i\"
ipsw_url = \"$IPSW_URL\"
ipsw_checksum = \"$IPSW_CHECKSUM\"
boot_wait = \"12m\"
create_vagrant_box = false" >macos.pkrvars.hcl

echo "Building the virtual machine"
/opt/homebrew/bin/packer build -var-file=macos.pkrvars.hcl .

packer_build_exit_code=$?
if [ $packer_build_exit_code -ne 0 ]; then
exit $packer_build_exit_code
fi

echo "Creating a directory for the virtual machine"
# Creating a directory for the virtual machine
mv "$USER_FOLDER/code/packer-examples/macos/out/$MACHINE_NAME-$i.macvm" "$USER_FOLDER/Parallels"

echo "Registering the virtual machine"
/usr/local/bin/prlctl register $USER_FOLDER/Parallels/$MACHINE_NAME-$i.macvm

echo "Starting the virtual machine"
/usr/local/bin/prlctl start "$MACHINE_NAME-$i"
done

echo "Cleaning up"
rm -rf ~/code
if [ -f ~/export.sh ]; then rm -f ~/export.sh; fi
92 changes: 92 additions & 0 deletions scripts/create-ubuntu-vm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/bin/bash

export PATH="/usr/local/bin:$PATH"
export USER_FOLDER=$(echo ~)
/Applications/Parallels\ Desktop.app/Contents/MacOS/Parallels\ Service service_restart

#check if export.sh exists
if [ -f ./export.sh ]; then
echo "export.sh file exists, setting the environment variables"
chmod +x ./export.sh
source ./export.sh
fi

if [ -z "$UBUNTU_VERSION" ]; then
UBUNTU_VERSION="22.04.3"
fi

if [ -z "$MACHINE_NAME" ]; then
MACHINE_NAME="Ubuntu Server $UBUNTU_VERSION"
fi

if [ -z "$MACHINE_COUNT" ]; then
MACHINE_COUNT=1
fi

if [ $MACHINE_COUNT -eq 0 ]; then
echo "MACHINE_COUNT is 0. Exiting..."
exit 0
fi

echo "Creating a directory for the code"
# Creating a directory for the code
if [ ! -d "$USER_FOLDER/code" ]; then
echo "Clone the packer-examples repo"
mkdir $USER_FOLDER/code
cd $USER_FOLDER/code

# clonning the packer-examples repo
git clone https://github.com/Parallels/packer-examples.git
fi

cd $USER_FOLDER/code/packer-examples/ubuntu

# Loop over the number of machines specified by $MACHINE_COUNT
for ((i = 1; i <= $MACHINE_COUNT; i++)); do
echo "Checking if the virtual machine $MACHINE_NAME-$i already exists"
# Checking if the virtual machine already exists
/usr/local/bin/prlctl list $MACHINE_NAME-$i
prlctl_exit_code=$?
if [ $prlctl_exit_code -eq 0 ]; then
continue
fi

if [ -f ubuntu.pkrvars.hcl ]; then
rm ubuntu.pkrvars.hcl
fi

echo "Initializing the packer project"
/opt/homebrew/bin/packer init .

echo "version = \"$UBUNTU_VERSION\"
machine_name = \"$MACHINE_NAME-$i\"
hostname = \"ubuntu-22-04-3-$i\"
machine_specs = {
cpus = $CPU_COUNT,
memory = $MEM_SIZE,
disk_size = \"$DISK_SIZE\",
}
create_vagrant_box = false" >ubuntu.pkrvars.hcl

echo "Building the virtual machine"
/opt/homebrew/bin/packer build -var-file=ubuntu.pkrvars.hcl .

packer_build_exit_code=$?
if [ $packer_build_exit_code -ne 0 ]; then
exit $packer_build_exit_code
fi

echo "Creating a directory for the virtual machine"
# Creating a directory for the virtual machine
mv "$USER_FOLDER/code/packer-examples/ubuntu/out/$MACHINE_NAME-$i.pvm" "$USER_FOLDER/Parallels"

echo "Registering the virtual machine"
/usr/local/bin/prlctl register $USER_FOLDER/Parallels/$MACHINE_NAME-$i.pvm

echo "Starting the virtual machine"
/usr/local/bin/prlctl start "$MACHINE_NAME-$i"
done

echo "Cleaning up"
rm -rf ~/code
if [ -f ~/export.sh ]; then rm -f ~/export.sh; fi
Loading

0 comments on commit fe4d5b3

Please sign in to comment.