Skip to content

Commit def5481

Browse files
committed
Initial import to GitHub
Signed-off-by: Samuli Seppänen <[email protected]> Signed-off-by: Samuli Seppänen <[email protected]>
1 parent b44bc69 commit def5481

12 files changed

+351
-2
lines changed

LICENSE.BSD

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright 2015 Samuli Seppänen and OpenVPN Technologies, Inc. (from this point
2+
onward "developers") and contributors. All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without modification,
5+
are permitted provided that the following conditions are met:
6+
7+
1. Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
10+
2. Redistributions in binary form must reproduce the above copyright notice,
11+
this list of conditions and the following disclaimer in the documentation and/or
12+
other materials provided with the distribution.
13+
14+
THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ''AS IS'' AND ANY EXPRESS OR IMPLIED
15+
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16+
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
17+
SHALL THE DEVELOPERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
18+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22+
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23+
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+
25+
The views and conclusions contained in the software and documentation are those
26+
of the authors and should not be interpreted as representing official policies,
27+
either expressed or implied, of the developers.

LICENSE.MIT

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2013-2014 Joe Fitzgerald
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+47-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,47 @@
1-
# scripts
2-
Various scripts for bootstrapping systems
1+
# Puppet-Finland scripts
2+
3+
A collection of useful scripts (shell and Powershell) used for bootstrapping
4+
systems. In this context bootstrapping means getting the node to a state where a
5+
real configuration management system (e.g. Puppet) can take over it's
6+
configuration.
7+
8+
These scripts are particularly useful with tools such as Vagrant for
9+
provisioning virtual machines.
10+
11+
These scripts can be run standalone for the most part. One strategy is to
12+
distribute a single site-specific initialization script that fetches and runs
13+
other bootstrapping scripts to do things like setup OpenVPN and then run Puppet.
14+
15+
# Project structure
16+
17+
Currently the project structure is simple:
18+
19+
.
20+
├── bootstrap
21+
│   ├── linux
22+
│   │   └── setup_puppet.ps1
23+
│   └── windows
24+
│   ├── config.ps1
25+
│   ├── create_user.ps1
26+
│   ├── init.bat
27+
│   ├── setup_openvpn.ps1
28+
│   ├── setup_puppet.ps1
29+
│   ├── setup_ssh_rsync.ps1
30+
│   ├── transcript_example.ps1
31+
│   └── utils.ps1
32+
└── README.md
33+
34+
The "bootstrap" directory contains all bootstrapping scripts. Scripts aimed at
35+
bootstrapping Linux (or *NIX) nodes are under "linux" and are typically (but not
36+
necessarily) written with sh shell in mind.
37+
38+
Powershell scripts and batch files used for bootstrapping Windows nodes are
39+
under the "windows" subdirectory. There are two special files: utils.ps1 and
40+
config.ps1. The former contains general purpose Powershell functions which are
41+
included in other Powershell scripts. The latter, config.ps1, contains common
42+
configuration for other Powershell scripts.
43+
44+
# License and copyrights
45+
46+
Unless otherwise stated the scripts use the BSD license (see LICENSE.BSD). Files
47+
taken from the packer-windows project use the MIT license (see LICENSE.MIT).

bootstrap/linux/setup_puppet.ps1

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
#
3+
# Setup Puppet on the node and do the first run
4+
5+
PATH=/bin:/usr/bin/
6+
7+
# Arguments from Vagrant
8+
LSBDISTCODENAME=$1
9+
FQDN=$2
10+
HOST=`echo $FQDN|cut -d "." -f 1`
11+
12+
# Install Puppet
13+
cd ~
14+
wget https://apt.puppetlabs.com/puppetlabs-release-$LSBDISTCODENAME.deb
15+
sudo dpkg -i puppetlabs-release-$LSBDISTCODENAME.deb
16+
sudo apt-get update
17+
sudo apt-get -y install puppet facter
18+
19+
# Set hostname/fqdn. Simply doing a "sudo echo" does not seem to work, hence the
20+
# trickery.
21+
sudo hostname $FQDN
22+
echo $FQDN|sudo tee /etc/hostname > /dev/null
23+
echo "127.0.1.1 $FQDN $HOST"|sudo tee -a /etc/hosts > /dev/null
24+
25+
# Setup puppet.conf
26+
if [ -r "/vagrant/puppet.conf" ]; then
27+
sudo cp /etc/puppet/puppet.conf /etc/puppet/puppet.conf.dist
28+
sudo cp /vagrant/puppet.conf /etc/puppet/puppet.conf
29+
fi
30+
31+
# Run Puppet
32+
puppet agent --test --waitforcert 60
33+
34+
# Install security updates
35+
apt-get update && apt-get dist-upgrade -y

bootstrap/windows/config.ps1

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### Parameters for scripts are stored in this file
2+
3+
# BASIC AUTH settings. These are used to securely fetch resources from
4+
# remote webservers.
5+
$filesBaseUrl = 'https://server.domain.com/protected/bootstrap'
6+
$filesHTTPUser = 'bootstrap'
7+
$filesHTTPPassword = 'somepassword'

bootstrap/windows/create_user.ps1

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
### Create the "vagrant" user
2+
net user vagrant "password" /ADD

bootstrap/windows/init.bat

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
rem Set Powershell ExecutionPolicy and run a Powershell initialization script
2+
@powershell -Command Set-ExecutionPolicy unrestricted
3+
powershell .\init.ps1

bootstrap/windows/setup_openvpn.ps1

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
### Install OpenVPN and connect to a VPN server to get access to intranet
2+
### resources
3+
4+
# Include other scripts to reduce repetition
5+
. ".\utils.ps1"
6+
. ".\config.ps1"
7+
8+
# (More or less) static variables
9+
$location = 'C:\Users\Administrator\bootstrap'
10+
$openvpnVersion = '2.3.6'
11+
$openvpnBuild = 'I603'
12+
$openvpnArch = 'x86_64'
13+
$openvpnBaseUrl = 'http://swupdate.openvpn.org/community/releases'
14+
$openvpnInstallerBaseName = "openvpn-install-$openvpnVersion-$openvpnBuild-$openvpnArch.exe"
15+
$openvpnUrl = "$openvpnBaseUrl/$openvpnInstallerBaseName"
16+
17+
$ovpnCertBaseName = 'openvpn.cer'
18+
$ovpnCertUrl = "$filesBaseUrl/windows/$ovpnCertBaseName"
19+
$ovpnConfBasename = 'client.ovpn'
20+
$ovpnConfUrl = "$filesBaseUrl/common/$ovpnConfBaseName"
21+
$ovpnPassBaseName = 'client.pass'
22+
$ovpnPassUrl = "$filesBaseUrl/common/$ovpnPassBaseName"
23+
24+
### Script begin
25+
New-Item -ItemType directory -Path $location -Force
26+
Set-Location $location
27+
28+
### Download files
29+
30+
Invoke-WebRequest -Uri $openvpnUrl -OutFile "$openvpnInstallerBaseName"
31+
Invoke-WebRequest -Uri $puppetUrl -OutFile $puppetInstallerBaseName
32+
download_file $ovpnCertUrl "$location\$ovpnCertBaseName"
33+
download_file $ovpnConfUrl "$location\$ovpnConfBaseName"
34+
download_file $ovpnPassUrl "$location\$ovpnPassBaseName"
35+
36+
### Setup OpenVPN
37+
38+
# Import OpenVPN publisher certificate so that tap-windows6 driver installation does not give a warning
39+
Import-Certificate -FilePath $ovpnCertBaseName -CertStoreLocation cert:\LocalMachine\TrustedPublisher
40+
41+
& $location\$openvpnInstallerBaseName /S | Out-Null
42+
43+
Copy-Item $ovpnConfBaseName "${Env:ProgramFiles}\openvpn\config\$ovpnConfBaseName"
44+
Copy-Item $ovpnPassBaseName "${Env:ProgramFiles}\openvpn\config\$ovpnPassBaseName"
45+
46+
Start-Service OpenVPNService
47+
Set-Service OpenVPNService -StartupType Automatic

bootstrap/windows/setup_puppet.ps1

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
### Setup Puppet and run Puppet Agent
2+
3+
# Include other scripts to reduce repetition
4+
. ".\utils.ps1"
5+
. ".\config.ps1"
6+
7+
# (More or less) static variables
8+
$location = 'C:\Users\Administrator\bootstrap'
9+
$puppetVersion = '3.7.4'
10+
$puppetArch = '-x64'
11+
$puppetBaseUrl = 'https://downloads.puppetlabs.com/windows'
12+
$puppetInstallerBaseName = "puppet-$puppetVersion$puppetArch.msi"
13+
$puppetUrl = "$puppetBaseUrl/$puppetInstallerBaseName"
14+
$puppetConfBasename = 'puppet.conf'
15+
$puppetConfUrl = "$filesBaseUrl/common/$puppetConfBaseName"
16+
$puppetBat = 'C:\Program Files\Puppet Labs\Puppet\bin\puppet.bat'
17+
18+
### Script begin
19+
20+
New-Item -ItemType directory -Path $location -Force
21+
Set-Location $location
22+
23+
### Download files
24+
25+
Invoke-WebRequest -Uri $puppetUrl -OutFile $puppetInstallerBaseName
26+
download_file $puppetConfUrl "$location\$puppetConfBaseName"
27+
28+
### Setup Puppet
29+
30+
& msiexec.exe /qn /i $location\$puppetInstallerBaseName | Out-Null
31+
32+
Copy-Item $puppetConfBaseName "${Env:ProgramData}\PuppetLabs\puppet\etc\$puppetConfBaseName"
33+
34+
& $puppetBat agent --test --waitforcert 10 --certname test.example.com | Out-Null

bootstrap/windows/setup_ssh_rsync.ps1

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
### Setup SSH and Rsync
2+
#
3+
# The script has been adapted from
4+
#
5+
# <https://github.com/joefitzgerald/packer-windows>
6+
7+
# This section is based on openssh.ps1 in https://github.com/joefitzgerald/packer-windows
8+
9+
$is_64bit = [IntPtr]::size -eq 8
10+
11+
# setup openssh
12+
$ssh_download_url = "http://www.mls-software.com/files/setupssh-6.6p1-1.exe"
13+
if ($is_64bit) {
14+
Write-Host "64 bit OS found"
15+
$ssh_download_url = "http://www.mls-software.com/files/setupssh-6.6p1-1(x64).exe"
16+
}
17+
18+
if (!(Test-Path "C:\Program Files\OpenSSH\bin\ssh.exe")) {
19+
Write-Host "Downloading $ssh_download_url"
20+
(New-Object System.Net.WebClient).DownloadFile($ssh_download_url, "C:\Windows\Temp\openssh.exe")
21+
Start-Process "C:\Windows\Temp\openssh.exe" "/S /port=22 /privsep=1 /password=D@rj33l1ng" -NoNewWindow -Wait
22+
}
23+
24+
Stop-Service "OpenSSHd" -Force
25+
26+
# ensure vagrant can log in
27+
Write-Host "Setting vagrant user file permissions"
28+
New-Item -ItemType Directory -Force -Path "C:\Users\vagrant\.ssh"
29+
C:\Windows\System32\icacls.exe "C:\Users\vagrant" /grant "vagrant:(OI)(CI)F"
30+
C:\Windows\System32\icacls.exe "C:\Program Files\OpenSSH\bin" /grant "vagrant:(OI)RX"
31+
C:\Windows\System32\icacls.exe "C:\Program Files\OpenSSH\usr\sbin" /grant "vagrant:(OI)RX"
32+
33+
Write-Host "Setting SSH home directories"
34+
(Get-Content "C:\Program Files\OpenSSH\etc\passwd") |
35+
Foreach-Object { $_ -replace '/home/(\w+)', '/cygdrive/c/Users/$1' } |
36+
Set-Content 'C:\Program Files\OpenSSH\etc\passwd'
37+
38+
# Set shell to /bin/sh to return exit status
39+
$passwd_file = Get-Content 'C:\Program Files\OpenSSH\etc\passwd'
40+
$passwd_file = $passwd_file -replace '/bin/bash', '/bin/sh'
41+
Set-Content 'C:\Program Files\OpenSSH\etc\passwd' $passwd_file
42+
43+
# fix opensshd to not be strict
44+
Write-Host "Setting OpenSSH to be non-strict"
45+
$sshd_config = Get-Content "C:\Program Files\OpenSSH\etc\sshd_config"
46+
$sshd_config = $sshd_config -replace 'StrictModes yes', 'StrictModes no'
47+
$sshd_config = $sshd_config -replace '#PubkeyAuthentication yes', 'PubkeyAuthentication yes'
48+
$sshd_config = $sshd_config -replace '#PermitUserEnvironment no', 'PermitUserEnvironment yes'
49+
# disable the use of DNS to speed up the time it takes to establish a connection
50+
$sshd_config = $sshd_config -replace '#UseDNS yes', 'UseDNS no'
51+
# disable the login banner
52+
$sshd_config = $sshd_config -replace 'Banner /etc/banner.txt', '#Banner /etc/banner.txt'
53+
Set-Content "C:\Program Files\OpenSSH\etc\sshd_config" $sshd_config
54+
55+
# use c:\Windows\Temp as /tmp location
56+
Write-Host "Setting temp directory location"
57+
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue "C:\Program Files\OpenSSH\tmp"
58+
C:\Program` Files\OpenSSH\bin\junction.exe /accepteula "C:\Program Files\OpenSSH\tmp" "C:\Windows\Temp"
59+
C:\Windows\System32\icacls.exe "C:\Windows\Temp" /grant "vagrant:(OI)(CI)F"
60+
61+
# add 64 bit environment variables missing from SSH
62+
Write-Host "Setting SSH environment"
63+
$sshenv = "TEMP=C:\Windows\Temp"
64+
if ($is_64bit) {
65+
$env_vars = "ProgramFiles(x86)=C:\Program Files (x86)", `
66+
"ProgramW6432=C:\Program Files", `
67+
"CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files", `
68+
"CommonProgramW6432=C:\Program Files\Common Files"
69+
$sshenv = $sshenv + "`r`n" + ($env_vars -join "`r`n")
70+
}
71+
Set-Content C:\Users\vagrant\.ssh\environment $sshenv
72+
73+
# record the path for provisioners (without the newline)
74+
Write-Host "Recording PATH for provisioners"
75+
Set-Content C:\Windows\Temp\PATH ([byte[]][char[]] $env:PATH) -Encoding Byte
76+
77+
# configure firewall
78+
Write-Host "Configuring firewall"
79+
netsh advfirewall firewall add rule name="SSHD" dir=in action=allow service=OpenSSHd enable=yes
80+
netsh advfirewall firewall add rule name="SSHD" dir=in action=allow program="C:\Program Files\OpenSSH\usr\sbin\sshd.exe" enable=yes
81+
netsh advfirewall firewall add rule name="ssh" dir=in action=allow protocol=TCP localport=22
82+
83+
Start-Service "OpenSSHd"
84+
85+
86+
# This section is adapted from rsync.bat in https://github.com/joefitzgerald/packer-windows
87+
88+
Set-Location C:\Windows\Temp
89+
90+
(New-Object System.Net.WebClient).DownloadFile('http://downloads.sourceforge.net/sevenzip/7z920-x64.msi', 'C:\Windows\Temp\7z920-x64.msi')
91+
msiexec /q /i C:\Windows\Temp\7z920-x64.msi
92+
93+
(New-Object System.Net.WebClient).DownloadFile('http://mirrors.kernel.org/sourceware/cygwin/x86_64/release/rsync/rsync-3.1.0-1.tar.xz', 'C:\Windows\Temp\rsync-3.1.0-1.tar.xz')
94+
C:\Program` Files\7-Zip\7z.exe x rsync-3.1.0-1.tar.xz
95+
C:\Program` Files\7-Zip\7z.exe x rsync-3.1.0-1.tar
96+
Copy-Item usr\bin\rsync.exe C:\Program` Files\OpenSSH\bin\rsync.exe
97+
98+
Remove-Item -Recurse -Path usr
99+
Remove-Item rsync-3.1.0-1.tar
100+
msiexec /q /x C:\Windows\Temp\7z920-x64.msi
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
### Example of Powershell script transcript and log
2+
3+
$logBaseName = "bootstrap-log.txt"
4+
$transcriptBaseName = "bootstrap-transcript.txt"
5+
6+
Start-Transcript -Path $transcriptBaseName -Force
7+
8+
Add-Content $logBaseName -value "Initializing"
9+
Add-Content $logBaseName -value "Downloading files"
10+
Add-Content $logBaseName -value "Setting up an application"
11+
12+
Stop-Transcript

bootstrap/windows/utils.ps1

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Invoke-WebRequest will fail with self-signed certificates:
2+
#
3+
# <https://connect.microsoft.com/PowerShell/feedback/details/419466/new-webserviceproxy-needs-force-parameter-to-ignore-ssl-errors>
4+
#
5+
# This solution is combined from a few of sources:
6+
#
7+
# <http://stackoverflow.com/questions/11696944/powershell-v3-invoke-webrequest-https-error/15841856>
8+
# <http://powershell.org/wp/forums/topic/trouble-with-invoke-webrequest>
9+
# <https://msdn.microsoft.com/en-us/library/system.net.webclient%28v=vs.110%29.aspx>
10+
#
11+
function download_file($url, $targetfile) {
12+
# In this case fully-qualified paths are necessary.
13+
$wc = New-Object System.Net.WebClient
14+
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
15+
$wc.Credentials = New-Object System.Net.NetworkCredential($filesHTTPUser, $filesHTTPPassword)
16+
$wc.DownloadFile($url, $targetfile)
17+
}

0 commit comments

Comments
 (0)