Skip to content

Commit

Permalink
wrapped action
Browse files Browse the repository at this point in the history
  • Loading branch information
vandot committed Oct 27, 2022
1 parent a9f696a commit 485e3e7
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 37 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Test lodns-action

on:
pull_request:
branches:
- '**'

jobs:
test:
strategy:
matrix:
os: [ubuntu-22.04, ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
name: Test lodns-action on ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: lodns-action
uses: ./
- name: Test server macOS
if: matrix.os == 'macos-latest'
run: |
dig @127.0.0.1 -p 5354 A example.lo
ping -c 1 example.lo
- name: Test server Ubuntu
if: matrix.os != 'macos-latest'
run: |
dig A example.lo
ping -c 1 example.lo
test-install-only:
runs-on: ubuntu-latest
name: Test lodns-action install-only
steps:
- name: Checkout
uses: actions/checkout@v3
- name: lodns-action
uses: ./
with:
install-only: true
- name: Test lodns binary
run: |
lodns-linux-amd64
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
## About

GitHub Action for [lodns](https://github.com/vandot/lodns), simple DNS server for local development.
___

* [Usage](#usage)
* [Customizing](#customizing)
* [inputs](#inputs)
* [Limitation](#limitation)
* [License](#license)

## Usage

```yaml
name: example

on:
push:

jobs:
example:
runs-on: ubuntu-latest
steps:
- uses: vandot/lodns-action@v1
```

## Customizing

### inputs

Following inputs can be used as `step.with` keys

| Name | Type | Default | Description |
|---------------|---------|-----------|---------------------------------|
| `version` | String | `0.1.3` | lodns version. Example: `0.1.3` |
| `install-only`| Bool | `false` | just install lodns |

## Limitation

This action is not available for Windows [virtual environment].
`lodns` supports Windows while this action still doesn't.

## License

BSD-3-Clause. See `LICENSE` for more details.
5 changes: 2 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# https://help.github.com/en/articles/metadata-syntax-for-github-actions
name: 'lodns Action'
name: 'lodns action'
description: 'GitHub Action for lodns, a simple DNS server for local development'
author: 'vandot'
branding:
Expand All @@ -9,7 +8,7 @@ branding:
inputs:
version:
description: 'lodns version'
default: 'latest'
default: '0.1.3'
required: false
install-only:
description: 'just install lodns'
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

88 changes: 70 additions & 18 deletions src/installer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import * as os from 'os';
import * as path from 'path';
import * as util from 'util';
import * as context from './context';
import * as github from './github';
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';
import {execSync} from 'child_process';
import {networkInterfaces} from 'os';

const osPlat: string = os.platform();
const osArch: string = os.arch();

export async function getLodns(version: string): Promise<string> {
export async function install(version: string): Promise<string> {
core.startGroup(`Checking lodns ${version} release...`);
const release: github.GitHubRelease | null = await github.getRelease(version);
if (!release) {
Expand All @@ -18,32 +17,85 @@ export async function getLodns(version: string): Promise<string> {
core.info(`lodns ${semver} found`);
core.endGroup();

const filename = util.format('%s%s', getName(semver), osPlat == 'win32' ? '.exe' : '');
const filename = util.format('%s.%s', getFilename(), context.osPlat == 'win32' ? 'zip' : 'tar.gz');
const downloadUrl = util.format('https://github.com/vandot/lodns/releases/download/%s/%s', semver, filename);

core.startGroup(`Downloading ${downloadUrl}...`);

const downloadPath: string = await tc.downloadTool(downloadUrl);
core.info(`Downloaded to ${downloadPath}`);

const cachePath: string = await tc.cacheDir(downloadPath, 'lodns-action', semver);
core.debug(`Cached to ${cachePath}`);
core.info('Extracting lodns');
let extPath: string;
if (context.osPlat == 'win32') {
extPath = await tc.extractZip(downloadPath);
} else {
extPath = await tc.extractTar(downloadPath);
}
core.debug(`Extracted to ${extPath}`);

const exePath: string = path.join(cachePath, getName(semver), osPlat == 'win32' ? '.exe' : '');
const cachePath: string = await tc.cacheDir(extPath, 'lodns-action', semver);
core.debug(`Cached to ${cachePath}`);
const exePath: string = path.join(cachePath, getFilename(), context.osPlat == 'win32' ? '.exe' : '');
core.debug(`Exe path is ${exePath}`);
core.endGroup();

return exePath;
}

function getName(version: string): string {
let platform = '';
if (osPlat == 'win32') {
platform = osArch == 'x64' ? 'win64' : 'win32';
} else if (osPlat == 'linux') {
platform = osArch == 'x64' ? 'amd64_linux' : 'i386_linux';
} else if (osPlat == 'darwin') {
platform = osArch == 'x64' ? 'amd64_linux' : 'i386_linux';
const getFilename = (): string => {
let arch: string;
let platform: string = context.osPlat;
switch (context.osArch) {
case 'x64': {
arch = 'amd64';
break;
}
case 'x32': {
throw new Error(`Unsupported Arch`);
}
case 'arm': {
throw new Error(`Unsupported Arch`);
}
default: {
arch = context.osArch;
break;
}
}
if (context.osPlat == 'win32') {
platform = 'windows';
}
return util.format('lodns-%s-%s', platform, arch);
};

export const useSudo = (): boolean => {
const version = execSync('systemd --version | head -1 | awk \'{print $2}\'');
let sudo: boolean = false;
if (context.osPlat == 'linux') {
if (Number(version) <= 245) {
sudo = true;
}
} else if (context.osPlat == 'win32') {
sudo = true;
}
return util.format('lodns-%s-%s', version, platform);
return sudo;
}

export async function ipSet(): Promise<boolean> {
return new Promise((cb) => {
const i = setInterval(() => {
let nets = networkInterfaces();
let [n] = nets.lodns0?.filter(x => x.address === '192.0.0.8') || []
if (n) {
clearInterval(i);
clearTimeout(t);
return cb(true);
}
}, 1000);

const t = setTimeout(() => {
clearInterval(i);
return cb(false);
}, 10000);
})
}
37 changes: 23 additions & 14 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as os from 'os';
import * as path from 'path';
import * as child_process from 'child_process';
import * as cp from 'child_process';
import * as context from './context';
import * as installer from './installer';
import * as core from '@actions/core';
Expand All @@ -9,28 +8,38 @@ import * as exec from '@actions/exec';
async function run(): Promise<void> {
try {
const inputs: context.Inputs = await context.getInputs();
const lodns = await installer.getLodns(inputs.version);
core.info(`lodns ${inputs.version} installed successfully`);
const lodns = await installer.install(inputs.version);
core.startGroup(`Installing lodns ${inputs.version}...`);

if (inputs.installOnly) {
const lodnsDir = path.dirname(lodns);
core.addPath(lodnsDir);
core.info(`lodns ${inputs.version} installed successfully`);
core.debug(`Added ${lodnsDir} to PATH`);
core.endGroup();
return;
}

await exec.exec(`${lodns} 'install'`, undefined);
await exec.exec(`sudo ${lodns} install`);

const child = child_process.spawn(lodns, ['start'], {
detached: true,
windowsHide: true,
shell: true,
stdio: [
'ignore'
]
})
if (context.osPlat == 'linux') {
const s = await installer.ipSet();
if (!s) {
throw new Error(`IP not set`);
}
}
core.info(`lodns ${inputs.version} installed successfully`);
core.endGroup();

child.unref()
core.startGroup(`Starting lodns...`);
var child: cp.ChildProcess
if (installer.useSudo()) {
child = cp.spawn('sudo', [lodns, 'start'], { detached: true, windowsHide: true, shell: true, stdio: 'ignore' });
} else {
child = cp.spawn(lodns, ['start'], { detached: true, windowsHide: true, shell: true, stdio: 'ignore' });
}
child.unref();
core.info(`lodns started`);

} catch (error) {
core.setFailed(error.message);
Expand Down

0 comments on commit 485e3e7

Please sign in to comment.