Google Cloud and Disk Snapshots for Testing

In my journey from Linux Admin to DevOps Engineer, it has been interesting to see the differences between on-premise and Google Cloud environments.

In both environments, Ansible was used to deploy and provision the virtual machine and required set of applications. Moving to the GCP environment, deploying VMs takes a bit longer than on-premise. There was a need to be able to quickly roll back a freshly deployed virtual machine for multiple tests.

This is an overview of the process for creating and rolling back to snapshots.

*Note: disk storage created in the cloud will have a cost, so be aware of your cloud budget when creating snapshots, and clean them up often.

# Log into gcloud auth
gcloud auth login

# Check the VM instance
gcloud beta compute instances list --filter="name~'$vmname'"

# Create a snapshot
gcloud compute disks snapshot $diskname --zone=$zonename --snapshot-names=$snapshotname --storage-location=$storagelocation --description="snapshot create for $vmname"

# List snapshots
gcloud compute snapshots list --filter="name~'^$snapshotname'" | awk '{print $1}' | cut -f 1

# Describe the VM instance in more detail
gcloud beta compute instances describe $vmname --zone=$zonename

# Shut down the VM before next steps
gcloud compute instances stop $vmname

# Detach the VM's disk
gcloud beta compute instances detach-disk $vmname --zone=$zonename --disk $diskname

# Create a new disk from the snapshot
gcloud compute disks create $diskname_new --source-snapshot $snapshotname --zone=$zonename

# Attach the disk created from previous step as the new bootable disk
gcloud beta compute instances attach-disk $vmname --disk $diskname_new --boot --zone=$zonename

# Configure disk auto-delete for the restored VM when destroyed
gcloud compute instances set-disk-auto-delete $vmname --disk=$diskname_new

# Delete an old disk - use caution - it will prompt you
gcloud compute disks delete $diskname --zone=$zonename

# Restart the VM
gcloud compute instances start $vmname --zone=$zonename

Join Mac OSX to Active Directory with Ansible

Joining Mac OSX to Active Directory can be a time-consuming process, with lots of pointing and clicking in the GUI. Being a member of AD is helpful to enable single sign on for users in a mostly Windows domain. But, if you use Ansible, it can only take seconds to rejoin. This is especially helpful when the Mac drops off the domain and needs to be quickly re-added.

See the Ansible playbook in github here:

The most important line is this:

command: "dsconfigad -force -add {{ DOMAIN }} -computer {{ ansible_hostname }}\
 -username {{ username }} -password {{ password }} -ou {{ OUSTRING }}\
 -mobile enable -mobileconfirm disable -localhome enable -useuncpath enable\
 -groups 'Domain Admins' -shell /bin/bash "

It uses the ‘dsconfigad’ utility to remove and rejoin the Mac’s computer account to Active Directory.

Developer environment: Atom, Git, and Windows

How to set up your developer environment if you are on Windows, and need to edit code in Gitlab/Github

Atom is a beautiful, open-source text editor that will let you browse the tree of your code, jump easily between folders, and see color-coded text to make things easier to read. If you are on Windows, you may feel that your environment does not look as cool as on Mac, but by using Atom, you will take things up a notch in coolness.

What problem does this solve? If you need to edit Unix code on Windows, you must save it natively as Windows, with the correct permissions and ownership. If you try other crossover tools such as Ubuntu Bash on Windows, you may run into issues with corrupted files since you are trying to edit from both your local editor and from the Unix emulated command line.

  1. Install Atom and Git-Plus plugin
  2. Install Git-Bash for Windows – this provides all the unix tools you will need.
  3. Install Pageant for ssh agent – also see this site
    1. Create an ssh key (passphrase protected)
    2. Upload your ssh key to your Gitlab/Github profile settings
    3. Configure plink.exe to be your ssh tool
    4. Create a shortcut that opens Pageant with your ssh key

Then you will be able to use Git-Bash for Windows to edit files in git via command line. Or, if you prefer the graphical environment, you can use Atom with the integrated Git-Plus or similar tools.

Example usage for this is to commit and push your git changes via Atom, then have your webapp do a git pull to synchronize the changes.

Tips for using Atom:

Open your project with Atom: Right-click, Add Project Folder

Git commands: Ctrl-Shift-A + S =  git status
Ctrl-S to save changes to the file (takes a second)
Ctrl-A or Ctrl-Shift-A – add
Ctrl-Shift-X Commit
Control-Shift-H for git-plus menu including Add and Commit and Push