Friday, 28 August 2015

Knife EC2 Winodws bootstrap using Ec2 --user-data option:


Assuming you have a basic knowledge about Chef bootstrapping.

Windows instance bootstrapping requires the winrm enabled images. Either you can create such images and create instance using those image and bootstrap it or use --user-data option to configure winrm while ec2 server create in chef.

Using knife-ec2 chef plugin you can achieve creating windows instance and bootstraping it.

For this you just have to write a proper bash powershell script and reference the path of your script file in --user-date option.

You can use following script to enable winrm and add winrm listener and then use knife-ec2 server create command with --user-data option.

users_data.ps1


<powershell>

$user="<username>"

$password="<password>"

net user /add $user $password;

net localgroup Administrators /add $user;

winrm quickconfig -q

winrm create winrm/config/Listener?Address=*+Transport=HTTP

winrm set winrm/config/service '@{AllowUnencrypted="true"}'

netsh advfirewall firewall set rule name="Windows Remote Management (HTTP-In)" profile=public protocol=tcp localport=5985 remoteip=localsubnet new remoteip=any

</powershell>


And run following command to create ec2 instance.

bundle exec knife ec2 server create -N <node-name> -I <windows-image-id> -f m1.small -x <username> -P <password> --ssh-key <ssh-key-name> -c <path-to-knfie,rb> --bootstrap-protocol winrm --user-data 'C:\\users_data.ps1'  -VV


Similar way you can write the Powershell script to add HTTPS Listener.

users_data.ps1


<powershell>
$user="<username>"
$password="<password>"
$vm_name="<node-name>"
net user /add $user $password;
net localgroup Administrators /add $user;
winrm quickconfig -q
New-SelfSignedCertificate -certstorelocation cert:\localmachine\my -dnsname $vm_name
$thumbprint = (Get-ChildItem -Path cert:\localmachine\my | Where-Object {$_.Subject -match "$vm_name"}).Thumbprint;
$create_listener_cmd = "winrm create winrm/config/Listener?Address=*+Transport=HTTPS '@{Hostname=`"$vm_name`";CertificateThumbprint=`"$thumbprint`"}'"
iex $create_listener_cmd
netsh advfirewall firewall add rule name="WinRM HTTPS" protocol=TCP dir=in Localport=5986 remoteport=any action=allow localip=any remoteip=any profile=public enable=yes
</powershell>




Wednesday, 8 July 2015

Bootstrap with chef-vault options


What is Chef Valut ?

Chef vault is alternative tool which allows you to securely manage passwords, certs, and other secrets in Chef. It was developed as an open source project originally by Nordstrom, but transferred to Chef in 2015. 

Using knife commands you can create and manage chef-valut

knife vault create passwords mysql '{"user":"root", "password" : "mysqlroot"}'  --mode client -c ~/chef-repo/.chef/knife.rb 

Note : by default the --mode is set to solo and your chef-vault databag will not get generated on chef-server so remember to set it to client.

You can get more details about knife vault option here https://github.com/Nordstrom/chef-vault


The data bag is generated on the chef server & each encrypted Chef-Vault data bag contains two items - a data bag containing the encrypted data & another data bag containing the public keys used to do the encryption.  

Following are the different vault options available for knife bootstrap:

--bootstrap-vault-file VAULT_FILE A JSON file with a list of vault(s) and item(s) to be updated


--bootstrap-vault-item VAULT_ITEM A single vault and item to update as "vault:item"




--bootstrap-vault-json VAULT_JSON A JSON string with the vault(s) and item(s) to be updated



Bootstrap examples with knife windows

knife bootstrap windows winrm <FQDN> -x <username> -P <password> -c <knife.rb path> --bootstrap-valut-file <path to valut file>

knife bootstrap windows winrm <FQDN> -x <username> -P <password> -c <knife.rb path> --bootstrap-vault-item '{"passwords": "mysql"}

knife bootstrap windows winrm <FQDN> -x <username> -P <password> -c <knife.rb path>
--bootstrap-vault-json '{"passwords": "mysql", "creds": "fooapi"}'