-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2bd3cf5
commit 7545363
Showing
4 changed files
with
189 additions
and
14 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
src/incus-app-container-files/cli/commands/ensure-vlan/mod.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { run, s, swallow } from "../../../deps.ts"; | ||
import { EnsureVlanInputOptions } from "../../config.ts"; | ||
import { BridgeName } from "../../things/bridge-name.ts"; | ||
import { | ||
calculateNicParentName, | ||
getNic, | ||
NicParentVlanName, | ||
} from "../../things/nic.ts"; | ||
import { | ||
createVlanEtcNetworkInterfacesD, | ||
type Vlan, | ||
} from "../../things/vlan.ts"; | ||
|
||
/** | ||
* Prints the current setpoint; the containers we want, according to configuration files. | ||
* @param options | ||
*/ | ||
export async function ensureVlan( | ||
options: EnsureVlanInputOptions, | ||
): Promise<void> { | ||
if (!options.bridgeName) { | ||
throw new Error("Missing --bridge-name."); | ||
} | ||
if (!options.vlan) { | ||
throw new Error("Missing --vlan."); | ||
} | ||
if (!options.file) { | ||
throw new Error("Missing --file."); | ||
} | ||
await actuallyEnsureVlan(options); | ||
console.log("{}"); | ||
} | ||
|
||
async function actuallyEnsureVlan( | ||
options: Required<EnsureVlanInputOptions>, | ||
): Promise<void> { | ||
const contents = createVlanEtcNetworkInterfacesD( | ||
options.bridgeName, | ||
options.vlan, | ||
); | ||
|
||
const nicName = calculateNicParentName( | ||
options.bridgeName, | ||
options.vlan, | ||
) as NicParentVlanName<BridgeName, Vlan>; | ||
|
||
const existingNic: string[] | undefined = await getNic(nicName); | ||
const existingFileContents: string | undefined = await Deno.readTextFile( | ||
options.file, | ||
).catch(swallow(Deno.errors.NotFound)); | ||
|
||
const wouldWill = options.dryRun ? "Would" : "Will"; | ||
|
||
if (existingNic) { | ||
console.error(`VLAN ${options.vlan} exists.`); | ||
if (existingFileContents === contents) { | ||
console.error( | ||
`The file ${options.file} matches. All good.`, | ||
); | ||
} else { | ||
console.error( | ||
`Any file ${options.file} does not match. ${wouldWill} take down VLAN ${options.vlan}, write the file, and bring up VLAN ${options.vlan}.`, | ||
); | ||
console.error({ current: s(existingFileContents), desired: s(contents) }); | ||
if (!options.dryRun) { | ||
await takeDownVlanOrSwallow(nicName); | ||
await writeVlanFile(options, contents); | ||
await bringUpVlanOrThrow(nicName); | ||
} | ||
} | ||
} else { | ||
console.error(`VLAN ${options.vlan} does not exist.`); | ||
if (existingFileContents === contents) { | ||
console.error( | ||
`The file ${options.file} matches. ${wouldWill} bring up VLAN ${options.vlan}.`, | ||
); | ||
if (!options.dryRun) { | ||
await bringUpVlanOrThrow(nicName); | ||
} | ||
} else { | ||
console.error( | ||
`Any file ${options.file} does not match. ${wouldWill} write the file, and bring up VLAN ${options.vlan}.`, | ||
); | ||
console.error({ current: s(existingFileContents), desired: s(contents) }); | ||
if (!options.dryRun) { | ||
await writeVlanFile(options, contents); | ||
await bringUpVlanOrThrow(nicName); | ||
} | ||
} | ||
} | ||
} | ||
|
||
async function writeVlanFile( | ||
options: Required<EnsureVlanInputOptions>, | ||
contents: string, | ||
): Promise<void> { | ||
await Deno.writeTextFile(options.file, contents); | ||
} | ||
|
||
async function bringUpVlanOrThrow( | ||
nicName: NicParentVlanName<BridgeName, Vlan>, | ||
): Promise<void> { | ||
await run(["ifup", nicName]); | ||
if (!await getNic(nicName)) { | ||
throw new Error(`Failed to bring up ${nicName}.`); | ||
} | ||
} | ||
|
||
async function takeDownVlanOrSwallow( | ||
nicName: NicParentVlanName<BridgeName, Vlan>, | ||
): Promise<void> { | ||
await run(["ifdown", nicName]).catch(swallow(Error)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters