Skip to content

Commit

Permalink
Put scripts back in index.js
Browse files Browse the repository at this point in the history
copy scripts back into index.js
This fixes running by other callers
  • Loading branch information
KnicKnic authored Jul 28, 2020
1 parent 6591bb6 commit 1a542f3
Show file tree
Hide file tree
Showing 2 changed files with 236 additions and 18 deletions.
127 changes: 118 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1595,6 +1595,12 @@ const path = __webpack_require__(622);
const os = __webpack_require__(87);
const fs = __webpack_require__(747);

function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}

// add format because that seems to be how github does formatting
String.prototype.format = function () {
Expand All @@ -1606,6 +1612,7 @@ String.prototype.format = function () {
}


// https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#using-a-specific-shell
let fileExtensions = {cmd: '.cmd', pwsh: '.ps1', powershell: '.ps1'}
let builtInShells = {
bash: 'bash --noprofile --norc -eo pipefail {0}',
Expand All @@ -1616,40 +1623,142 @@ let builtInShells = {
powershell: 'powershell -command "& \'{0}\'"',
}

let linuxScriptName = path.join(process.env.GITHUB_WORKSPACE, "linux.sh")
let linuxScript = `
#!/bin/bash
curl -sfL https://get.k3s.io | sh -s - --docker
mkdir ~/.kube || echo "~/.kube already existed"
sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
sudo chmod 777 ~/.kube/config
# systemctl status k3s
# sleep 15
cat ~/.kube/config
kubectl get node
`

let linuxShell = 'bash'

let windowsScriptName = path.join(process.env.GITHUB_WORKSPACE, "windows.ps1")
let windowsScript = `
$url_file = "https://github.com/KnicKnic/k3s/releases/download/files2/files.zip"
$work_dir = $env:GITHUB_WORKSPACE
$k3s_path = join-path $work_dir "k3s.exe"
$k3s_tmp_dir = join-path $work_dir "k3s_tmp"
$logs_file = join-path $k3s_tmp_dir "logs.txt"
$zip_file = join-path $work_dir "files.zip"
function logMessage($msg){
$str = "{0} logMessage {1}" -f $(get-date), $msg
echo $str
}
logMessage "start download"
curl.exe -s -L -o $zip_file $url_file
logMessage "start extract"
Expand-Archive -Path $zip_file -DestinationPath $work_dir
$env:Path += ";$work_dir"
mkdir /etc -ErrorAction SilentlyContinue
mkdir $k3s_tmp_dir -ErrorAction SilentlyContinue
#setup environment
echo """
nameserver 8.8.8.8
""" > /etc/resolv.conf
logMessage "Get Ip info"
ipconfig /all
$hostNetwork = get-NetIPAddress -InterfaceAlias "Ethernet"| ?{$_.AddressFamily -eq "IPv4"}
$env:hostIp = $hostNetwork.IpAddress
$env:hostCidr = "{0}/{1}" -f $hostNetwork.IpAddress, $hostNetwork.PrefixLength
#for host-gw
#eventually need to get rid of KUBE_NETWORK
$env:KUBE_NETWORK="cbr0"
# Ideally I would directly launch k3s like 2 lines below, however when I do, pwsh gets wedged
start-process "pwsh.exe" -ArgumentList @("-command", "$k3s_path server -d $k3s_tmp_dir --flannel-backend host-gw --docker --disable-network-policy --pause-image mcr.microsoft.com/k8s/core/pause:1.0.0 --disable servicelb,traefik,local-storage,metrics-server 2>&1 > $logs_file")
# $arguments = "server -d $k3s_tmp_dir --flannel-backend host-gw --docker --disable-network-policy --pause-image mcr.microsoft.com/k8s/core/pause:1.0.0 --disable servicelb,traefik,local-storage,metrics-server".Split()
# start-process $k3s_path -ArgumentList $arguments -RedirectStandardError $logs_file
foreach ($seconds in 1..120) {
logMessage "waiting for ~/.kube/k3s.yaml"
$found = test-path "~/.kube/k3s.yaml"
sleep 1; # always sleep (?give time for file to flush?)
if($found)
{
break;
}
}
logMessage "copying kubeconfig"
copy "~/.kube/k3s.yaml" "~/.kube/config"
foreach ($seconds in 1..120) {
$empty = kubectl get node $env:COMPUTERNAME
if($LASTEXITCODE -eq 0)
{
break;
}
sleep 1;
}
logMessage "node exists"
type $logs_file
kubectl get node
logMessage "done"
`

let windowsShell = 'pwsh'

async function body() {
try{
let unformattedShell = '';
let unformattedShell = ''
let command = ''
let file = path.join(process.env.GITHUB_WORKSPACE, uuidv4())

let platform = os.platform()
// if(platform == 'darwin'){
// file = macosScriptName
// unformattedShell = macosShell
// command = macosScript
// unformattedShell = macosShell
// }

if(platform == 'linux'){
file = linuxScriptName
command = linuxScript
unformattedShell = linuxShell
}
else if (platform == 'win32'){
file = windowsScriptName
command = windowsScript
unformattedShell = windowsShell
} else{
core.setFailed("Unsupported os " + platform)
}


let fileExtension = fileExtensions[unformattedShell] || ''
file = file+fileExtension

let shell = builtInShells[unformattedShell] || unformattedShell
let formattedShell = shell.format(file)

core.info(`About to run command ${file}`)
fs.writeFileSync(file, command)
core.info(`About to run command ${command}`)

const error_code = await exec.exec(formattedShell);

Expand Down
127 changes: 118 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ const path = require('path');
const os = require('os');
const fs = require('fs');

function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}

// add format because that seems to be how github does formatting
String.prototype.format = function () {
Expand All @@ -18,6 +24,7 @@ String.prototype.format = function () {
}


// https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#using-a-specific-shell
let fileExtensions = {cmd: '.cmd', pwsh: '.ps1', powershell: '.ps1'}
let builtInShells = {
bash: 'bash --noprofile --norc -eo pipefail {0}',
Expand All @@ -28,40 +35,142 @@ let builtInShells = {
powershell: 'powershell -command "& \'{0}\'"',
}

let linuxScriptName = path.join(process.env.GITHUB_WORKSPACE, "linux.sh")
let linuxScript = `
#!/bin/bash
curl -sfL https://get.k3s.io | sh -s - --docker
mkdir ~/.kube || echo "~/.kube already existed"
sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
sudo chmod 777 ~/.kube/config
# systemctl status k3s
# sleep 15
cat ~/.kube/config
kubectl get node
`

let linuxShell = 'bash'

let windowsScriptName = path.join(process.env.GITHUB_WORKSPACE, "windows.ps1")
let windowsScript = `
$url_file = "https://github.com/KnicKnic/k3s/releases/download/files2/files.zip"
$work_dir = $env:GITHUB_WORKSPACE
$k3s_path = join-path $work_dir "k3s.exe"
$k3s_tmp_dir = join-path $work_dir "k3s_tmp"
$logs_file = join-path $k3s_tmp_dir "logs.txt"
$zip_file = join-path $work_dir "files.zip"
function logMessage($msg){
$str = "{0} logMessage {1}" -f $(get-date), $msg
echo $str
}
logMessage "start download"
curl.exe -s -L -o $zip_file $url_file
logMessage "start extract"
Expand-Archive -Path $zip_file -DestinationPath $work_dir
$env:Path += ";$work_dir"
mkdir /etc -ErrorAction SilentlyContinue
mkdir $k3s_tmp_dir -ErrorAction SilentlyContinue
#setup environment
echo """
nameserver 8.8.8.8
""" > /etc/resolv.conf
logMessage "Get Ip info"
ipconfig /all
$hostNetwork = get-NetIPAddress -InterfaceAlias "Ethernet"| ?{$_.AddressFamily -eq "IPv4"}
$env:hostIp = $hostNetwork.IpAddress
$env:hostCidr = "{0}/{1}" -f $hostNetwork.IpAddress, $hostNetwork.PrefixLength
#for host-gw
#eventually need to get rid of KUBE_NETWORK
$env:KUBE_NETWORK="cbr0"
# Ideally I would directly launch k3s like 2 lines below, however when I do, pwsh gets wedged
start-process "pwsh.exe" -ArgumentList @("-command", "$k3s_path server -d $k3s_tmp_dir --flannel-backend host-gw --docker --disable-network-policy --pause-image mcr.microsoft.com/k8s/core/pause:1.0.0 --disable servicelb,traefik,local-storage,metrics-server 2>&1 > $logs_file")
# $arguments = "server -d $k3s_tmp_dir --flannel-backend host-gw --docker --disable-network-policy --pause-image mcr.microsoft.com/k8s/core/pause:1.0.0 --disable servicelb,traefik,local-storage,metrics-server".Split()
# start-process $k3s_path -ArgumentList $arguments -RedirectStandardError $logs_file
foreach ($seconds in 1..120) {
logMessage "waiting for ~/.kube/k3s.yaml"
$found = test-path "~/.kube/k3s.yaml"
sleep 1; # always sleep (?give time for file to flush?)
if($found)
{
break;
}
}
logMessage "copying kubeconfig"
copy "~/.kube/k3s.yaml" "~/.kube/config"
foreach ($seconds in 1..120) {
$empty = kubectl get node $env:COMPUTERNAME
if($LASTEXITCODE -eq 0)
{
break;
}
sleep 1;
}
logMessage "node exists"
type $logs_file
kubectl get node
logMessage "done"
`

let windowsShell = 'pwsh'

async function body() {
try{
let unformattedShell = '';
let unformattedShell = ''
let command = ''
let file = path.join(process.env.GITHUB_WORKSPACE, uuidv4())

let platform = os.platform()
// if(platform == 'darwin'){
// file = macosScriptName
// unformattedShell = macosShell
// command = macosScript
// unformattedShell = macosShell
// }

if(platform == 'linux'){
file = linuxScriptName
command = linuxScript
unformattedShell = linuxShell
}
else if (platform == 'win32'){
file = windowsScriptName
command = windowsScript
unformattedShell = windowsShell
} else{
core.setFailed("Unsupported os " + platform)
}


let fileExtension = fileExtensions[unformattedShell] || ''
file = file+fileExtension

let shell = builtInShells[unformattedShell] || unformattedShell
let formattedShell = shell.format(file)

core.info(`About to run command ${file}`)
fs.writeFileSync(file, command)
core.info(`About to run command ${command}`)

const error_code = await exec.exec(formattedShell);

Expand Down

0 comments on commit 1a542f3

Please sign in to comment.