gibil 0.1.7 → 0.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -4
- package/dist/index.js +23 -19
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
<p align="center"><strong>Full servers. Forged in seconds. Gone when done.</strong></p>
|
|
8
8
|
|
|
9
9
|
<p align="center">
|
|
10
|
-
<img src="https://img.shields.io/badge/version-0.1.
|
|
11
|
-
<img src="https://img.shields.io/badge/tests-
|
|
10
|
+
<img src="https://img.shields.io/badge/version-0.1.9-blue" alt="Version 0.1.9" />
|
|
11
|
+
<img src="https://img.shields.io/badge/tests-128%20passing-brightgreen" alt="Tests: 128 passing" />
|
|
12
12
|
<img src="https://img.shields.io/badge/typescript-%3E5.0-3178C6" alt="TypeScript" />
|
|
13
13
|
<img src="https://img.shields.io/badge/node-%3E%3D20-339933" alt="Node >= 20" />
|
|
14
14
|
<img src="https://img.shields.io/badge/license-proprietary-red" alt="License: Proprietary" />
|
|
@@ -61,11 +61,13 @@ gibil destroy my-app
|
|
|
61
61
|
|
|
62
62
|
| Command | Description |
|
|
63
63
|
|---------|-------------|
|
|
64
|
+
| `gibil init` | Set up gibil — Hetzner token, MCP config, agent skill |
|
|
64
65
|
| `gibil create` | Forge an ephemeral server |
|
|
65
66
|
| `gibil ssh <name>` | SSH into a running server |
|
|
66
|
-
| `gibil run <name> <cmd>` | Execute a command remotely |
|
|
67
|
+
| `gibil run <name> <cmd>` | Execute a command remotely (`--background` for async) |
|
|
68
|
+
| `gibil job <cmd>` | Manage background jobs (status, list, cancel, logs) |
|
|
67
69
|
| `gibil exec <name>` | Upload and run a local script |
|
|
68
|
-
| `gibil mcp
|
|
70
|
+
| `gibil mcp [name]` | Start MCP server for AI agents |
|
|
69
71
|
| `gibil list` | List all active servers |
|
|
70
72
|
| `gibil extend <name>` | Extend a server's TTL |
|
|
71
73
|
| `gibil destroy [name]` | Burn down a server |
|
package/dist/index.js
CHANGED
|
@@ -1,23 +1,27 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
var
|
|
3
|
-
${
|
|
4
|
-
${
|
|
5
|
-
${
|
|
6
|
-
${
|
|
2
|
+
var Re=Object.defineProperty;var Ct=(e,t)=>()=>(e&&(t=e(e=0)),t);var Bt=(e,t)=>{for(var n in t)Re(e,n,{get:t[n],enumerable:!0})};import{homedir as Ge}from"os";import{join as L}from"path";var D,S,M=Ct(()=>{"use strict";D=L(Ge(),".gibil"),S={root:D,instances:L(D,"instances"),keys:L(D,"keys"),jobs:L(D,"jobs"),instanceFile:e=>L(D,"instances",`${e}.json`),keyDir:e=>L(D,"keys",e),privateKey:e=>L(D,"keys",e,"id_ed25519"),publicKey:e=>L(D,"keys",e,"id_ed25519.pub")}});var Mt={};Bt(Mt,{clearApiKey:()=>Tt,fetchUsage:()=>Ht,getApiKey:()=>P,getApiUrl:()=>qe,getApiUrlFromConfig:()=>pt,getHetznerToken:()=>Ot,getServerDefaults:()=>We,saveApiKey:()=>ot,saveHetznerToken:()=>rt,saveServerDefaults:()=>Nt,trackUsage:()=>X,verifyApiKey:()=>F});import{readFile as Fe,writeFile as Je,mkdir as ze}from"fs/promises";import{existsSync as Ue}from"fs";import{join as Be}from"path";async function G(){if(!Ue(At))return{};let e=await Fe(At,"utf-8");return JSON.parse(e)}async function dt(e){await ze(S.root,{recursive:!0,mode:448}),await Je(At,JSON.stringify(e,null,2),{mode:384})}async function ot(e){let t=await G();t.api_key=e,await dt(t)}async function P(){return process.env.GIBIL_API_KEY?process.env.GIBIL_API_KEY:(await G()).api_key??null}async function Tt(){let e=await G();delete e.api_key,await dt(e)}function qe(){return process.env.GIBIL_API_URL??Qt}async function pt(){return process.env.GIBIL_API_URL?process.env.GIBIL_API_URL:(await G()).api_url??Qt}async function rt(e){let t=await G();t.hetzner_token=e,await dt(t)}async function Ot(){return process.env.HETZNER_API_TOKEN?process.env.HETZNER_API_TOKEN:(await G()).hetzner_token??null}async function Nt(e,t){let n=await G();n.default_server_type=e,n.default_location=t,await dt(n)}async function We(){let e=await G();return{serverType:e.default_server_type??"cax11",location:e.default_location??"fsn1"}}async function F(e){let t=await pt(),n=await fetch(`${t}/auth-verify`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({api_key:e})});if(n.status===401)throw new Error("Invalid API key. Get one at https://gibil.dev");if(!n.ok){let r=await n.text();throw new Error(`API error (${n.status}): ${r}`)}return await n.json()}async function X(e,t,n,r){let o=await pt(),c=await fetch(`${o}/usage-track`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({api_key:e,event:t,instance_name:n,server_type:r})});if(c.status===429)throw new Error("Plan limit reached. Upgrade at https://gibil.dev/pricing");if(!c.ok){let i=await c.text();throw new Error(`Usage tracking failed (${c.status}): ${i}`)}}async function Ht(e){let t=await pt(),n=await fetch(`${t}/usage-get`,{headers:{Authorization:`Bearer ${e}`}});if(!n.ok){let r=await n.text();throw new Error(`Failed to fetch usage (${n.status}): ${r}`)}return await n.json()}var At,Qt,J=Ct(()=>{"use strict";M();At=Be(S.root,"config.json"),Qt="https://zopdxjruwktjyjunitrv.supabase.co/functions/v1"});var be={};Bt(be,{JobStore:()=>xt,deleteJob:()=>jn,deleteJobsByInstance:()=>Lt,listJobs:()=>at,listJobsByInstance:()=>Cn,loadJob:()=>_n,loadJobOrThrow:()=>U,saveJob:()=>z});import{readFile as xn,writeFile as Sn,mkdir as fe,rm as In,readdir as kn}from"fs/promises";import{existsSync as he}from"fs";import{join as ye}from"path";var xt,V,z,_n,U,jn,at,Cn,Lt,tt=Ct(()=>{"use strict";M();xt=class{jobsDir;constructor(t){let n=t??S.root;this.jobsDir=ye(n,"jobs")}jobFile(t){return ye(this.jobsDir,`${t}.json`)}async save(t){await fe(this.jobsDir,{recursive:!0,mode:448}),await Sn(this.jobFile(t.id),JSON.stringify(t,null,2),{mode:384})}async load(t){let n=this.jobFile(t);if(!he(n))return null;let r=await xn(n,"utf-8");return JSON.parse(r)}async loadOrThrow(t){let n=await this.load(t);if(!n)throw new Error(`Job "${t}" not found. Run "gibil job list" to see active jobs.`);return n}async delete(t){let n=this.jobFile(t);he(n)&&await In(n)}async list(){await fe(this.jobsDir,{recursive:!0,mode:448});let t=await kn(this.jobsDir),n=[];for(let r of t){if(!r.endsWith(".json"))continue;let o=r.replace(".json",""),c=await this.load(o);c&&n.push(c)}return n}async listByInstance(t){return(await this.list()).filter(r=>r.instance===t)}async deleteByInstance(t){let n=await this.listByInstance(t);for(let r of n)await this.delete(r.id)}},V=new xt,z=e=>V.save(e),_n=e=>V.load(e),U=e=>V.loadOrThrow(e),jn=e=>V.delete(e),at=()=>V.list(),Cn=e=>V.listByInstance(e),Lt=e=>V.deleteByInstance(e)});import{Command as Un}from"commander";import{readFileSync as Bn}from"fs";import{fileURLToPath as qn}from"url";import{dirname as Wn,join as Yn}from"path";import Z from"picocolors";var R=e=>Z.red(e),Ke=e=>Z.yellow(e),q=e=>Z.green(e),De=e=>Z.red(e),p=e=>Z.dim(e),v=e=>Z.bold(e),E="\u{1F98E}";var H=q("\u2713"),nt=De("\u2716"),Wt=Ke("\u26A0"),ut="\u{12248}",Yt=`
|
|
3
|
+
${R(" /\\")}
|
|
4
|
+
${R(" / \\")}
|
|
5
|
+
${R(" / \u{1F525} \\")}
|
|
6
|
+
${R(" / \\")}
|
|
7
7
|
${p(" ~~~~~~~~")}
|
|
8
|
-
${
|
|
9
|
-
`,
|
|
10
|
-
`),this)}update(
|
|
11
|
-
`)}succeed(
|
|
12
|
-
`)}fail(
|
|
13
|
-
`)}stop(){this.timer&&(clearInterval(this.timer),this.timer=null,process.stderr.isTTY&&process.stderr.write("\r\x1B[K"))}};function
|
|
14
|
-
`)}function
|
|
15
|
-
`)}function Kt(t){let e=[];if(t.startsWith("node:")){let n=t.split(":")[1]??"20";e.push("# Install Node.js"),e.push(`curl -fsSL https://deb.nodesource.com/setup_${n}.x | bash - > /dev/null 2>&1`),e.push("apt-get install -y -qq nodejs > /dev/null 2>&1"),e.push("npm install -g pnpm@latest > /dev/null 2>&1"),e.push("")}else if(t.startsWith("python:")){let n=t.split(":")[1]??"3.12";e.push("# Install Python"),e.push(`apt-get install -y -qq python${n} python3-pip python3-venv > /dev/null 2>&1`),e.push("")}else if(t.startsWith("go:")){let n=t.split(":")[1]??"1.22";e.push("# Install Go"),e.push('GO_ARCH=$(uname -m | sed "s/x86_64/amd64/" | sed "s/aarch64/arm64/")'),e.push(`wget -q https://go.dev/dl/go${n}.linux-\${GO_ARCH}.tar.gz -O /tmp/go.tar.gz`),e.push("tar -C /usr/local -xzf /tmp/go.tar.gz"),e.push("export PATH=$PATH:/usr/local/go/bin"),e.push('echo "export PATH=\\$PATH:/usr/local/go/bin" >> /root/.bashrc'),e.push("")}else e.push("# Install Node.js (default)"),e.push("curl -fsSL https://deb.nodesource.com/setup_20.x | bash - > /dev/null 2>&1"),e.push("apt-get install -y -qq nodejs > /dev/null 2>&1"),e.push("npm install -g pnpm@latest > /dev/null 2>&1"),e.push("");return e}function Rt(){return["# Install Docker","curl -fsSL https://get.docker.com | sh > /dev/null 2>&1","systemctl enable docker --now"]}function Lt(t){let e=[];e.push(`# Start service: ${t.name.replace(/[^a-zA-Z0-9_-]/g,"")}`);let s=`docker run -d --name ${t.name.replace(/[^a-zA-Z0-9_-]/g,"")}`;if(t.port&&(s+=` -p ${t.port}:${t.port}`),t.env)for(let[i,c]of Object.entries(t.env))s+=` -e ${i}=${T(c)}`;return s+=` ${t.image}`,e.push(s),e.push(""),e}function T(t){return`'${t.replace(/'/g,"'\\''")}'`}import{readFile as Mt}from"fs/promises";import{existsSync as Re,statSync as Dt}from"fs";import{join as zt}from"path";import{parse as Me}from"yaml";var Gt=".gibil.yml";async function le(t){let e;if(Re(t)&&Dt(t).isFile()?e=t:e=zt(t,Gt),!Re(e))return null;let n=await Mt(e,"utf-8"),s=Me(n);return ze(s)}function De(t){let e=Me(t);return ze(e)}function ze(t){if(!t||typeof t!="object")throw new Error("Invalid .gibil.yml: must be a YAML object");let e=t,n={};return typeof e.name=="string"&&(n.name=e.name),typeof e.image=="string"&&(n.image=e.image),typeof e.server_type=="string"&&(n.server_type=e.server_type),typeof e.location=="string"&&(n.location=e.location),Array.isArray(e.services)&&(n.services=e.services.map(s=>{let i=s;if(typeof i.name!="string"||typeof i.image!="string")throw new Error("Each service must have a 'name' and 'image' field");return{name:i.name,image:i.image,port:typeof i.port=="number"?i.port:void 0,env:Le(i.env,`service "${i.name}"`)}})),Array.isArray(e.tasks)&&(n.tasks=e.tasks.map(s=>{let i=s;if(typeof i.name!="string"||typeof i.command!="string")throw new Error("Each task must have a 'name' and 'command' field");return{name:i.name,command:i.command}})),e.env!==void 0&&(n.env=Le(e.env,"top-level")),n}function Le(t,e){if(t==null)return;if(typeof t!="object"||Array.isArray(t))throw new Error(`env in ${e} must be a key-value object`);let n={};for(let[s,i]of Object.entries(t))if(typeof i=="string")n[s]=i;else if(typeof i=="number"||typeof i=="boolean")n[s]=String(i);else throw new Error(`env.${s} in ${e} must be a string, number, or boolean \u2014 got ${typeof i}`);return Object.keys(n).length>0?n:void 0}R();import{readFile as Ut,writeFile as Ft,mkdir as Ge,rm as Bt,readdir as qt}from"fs/promises";import{existsSync as Ue}from"fs";import{join as xe}from"path";var Se=class{instancesDir;keysDir;constructor(e){let n=e??w.root;this.instancesDir=xe(n,"instances"),this.keysDir=xe(n,"keys")}async ensureDirectories(){await Ge(this.instancesDir,{recursive:!0,mode:448}),await Ge(this.keysDir,{recursive:!0,mode:448})}instanceFile(e){return xe(this.instancesDir,`${e}.json`)}async save(e){await this.ensureDirectories(),await Ft(this.instanceFile(e.name),JSON.stringify(e,null,2),{mode:384})}async load(e){let n=this.instanceFile(e);if(!Ue(n))return null;let s=await Ut(n,"utf-8");return JSON.parse(s)}async loadOrThrow(e){let n=await this.load(e);if(!n)throw new Error(`Instance "${e}" not found. Run "gibil list" to see active instances.`);return n}async loadActiveOrThrow(e){let n=await this.loadOrThrow(e);if(new Date>new Date(n.expiresAt))throw new Error(`Instance "${e}" has expired (TTL was ${n.ttlMinutes}m). Run "gibil destroy ${e}" to clean up.`);return n}async delete(e){let n=this.instanceFile(e);Ue(n)&&await Bt(n)}async list(){await this.ensureDirectories();let e=await qt(this.instancesDir),n=[];for(let s of e){if(!s.endsWith(".json"))continue;let i=s.replace(".json",""),c=await this.load(i);c&&n.push(c)}return n}},ee=new Se;var F=t=>ee.save(t);var Fe=t=>ee.loadOrThrow(t),E=t=>ee.loadActiveOrThrow(t),ue=t=>ee.delete(t),B=()=>ee.list();import{randomBytes as Jt}from"crypto";function Be(t=6){return Jt(Math.ceil(t/2)).toString("hex").slice(0,t)}function pe(){return`gibil-${Be()}`}function qe(){return`fleet-${Be(8)}`}R();var Wt=/^[a-zA-Z0-9][a-zA-Z0-9_-]{0,62}$/;function Je(t){if(!Wt.test(t))throw new Error(`Invalid instance name "${t}". Names must be 1-63 chars, start with alphanumeric, and contain only [a-zA-Z0-9_-].`);return t}function te(t,e){let n=parseInt(t,10);if(isNaN(n)||n<=0)throw new Error(`${e} must be a positive integer, got "${t}"`);return n}D();import{execSync as de}from"child_process";import{readFileSync as Yt}from"fs";function Vt(){try{let t=de("git config user.name",{encoding:"utf-8"}).trim(),e=de("git config user.email",{encoding:"utf-8"}).trim();if(!t||!e)return;let n;try{if(de("git config gpg.format",{encoding:"utf-8"}).trim()==="ssh"){let i=de("git config user.signingkey",{encoding:"utf-8"}).trim();if(i)try{n=Yt(i,"utf-8").trim()}catch{(i.startsWith("ssh-")||i.startsWith("key::"))&&(n=i.replace(/^key::/,""))}}}catch{}return{name:t,email:e,signingKey:n}}catch{return}}async function We(t,e,n){o.step("Generating SSH keys...");let s=await se(e),i;try{o.step("Uploading SSH key..."),i=await t.createSSHKey(`gibil-${e}`,s.publicKey),n.repo&&n.repo.includes("github.com")&&!process.env.GITHUB_TOKEN&&o.debug("No GITHUB_TOKEN set \u2014 private repos will fail to clone. Set GITHUB_TOKEN to enable private repo access.");let c=Vt(),r=ce({repo:n.repo,config:n.config??void 0,ttlMinutes:n.ttlMinutes,githubToken:process.env.GITHUB_TOKEN,gitIdentity:c});o.step("Creating server...");let l=await t.createServer(e,i.id,r,n.serverType??n.config?.server_type,n.location??n.config?.location);o.step("Waiting for server...");let u=(await t.waitForReady(l.id)).public_net.ipv4.ip,f=new Date,g={name:e,serverId:l.id,ip:u,sshKeyId:i.id,keyPath:w.privateKey(e),status:"running",createdAt:f.toISOString(),ttlMinutes:n.ttlMinutes,expiresAt:new Date(f.getTime()+n.ttlMinutes*6e4).toISOString(),repo:n.repo,fleetId:n.fleetId,gitIdentity:c};if(await F(g),o.step(`Waiting for SSH on ${u}...`),await ae(e,u),n.repo||n.config){o.step("Waiting for provisioning...");let d=36e4,m=5e3,v=Date.now(),S=!1;for(;Date.now()-v<d;){try{if((await C({instanceName:e,ip:u,command:"test -f /root/.gibil-ready && echo ready || echo waiting",timeoutMs:1e4})).stdout.trim()==="ready"){S=!0;break}}catch{}await new Promise(O=>setTimeout(O,m))}if(!S)try{let O=await C({instanceName:e,ip:u,command:"tail -20 /var/log/cloud-init-output.log 2>/dev/null || echo 'No cloud-init log found'",timeoutMs:1e4});o.warn("Provisioning may have failed. Cloud-init log:"),o.info(O.stdout)}catch{o.warn("Provisioning may have failed \u2014 could not read cloud-init log.")}}return g}catch(c){if(o.error(`Failed to create instance "${e}", cleaning up...`),await Y(e).catch(r=>o.warn(`Could not clean up SSH keys: ${r instanceof Error?r.message:String(r)}`)),i){let r=i.id;await t.deleteSSHKey(r).catch(l=>o.warn(`Could not delete Hetzner SSH key ${r}: ${l instanceof Error?l.message:String(l)}`))}throw c}}function Ye(t){let e=Math.max(0,Math.floor((new Date(t.expiresAt).getTime()-Date.now())/1e3));return{name:t.name,ip:t.ip,ssh:`ssh -i ${t.keyPath} -o StrictHostKeyChecking=no root@${t.ip}`,status:t.status,ttl_remaining:e,created_at:t.createdAt,fleet_id:t.fleetId}}async function Zt(t){let e=t.match(/github\.com\/([^/]+)\/([^/.]+)/);if(!e)return o.debug(`Cannot fetch config from non-GitHub repo: ${t}`),null;let[,n,s]=e,i=`https://raw.githubusercontent.com/${n}/${s}/HEAD/.gibil.yml`;o.debug(`Fetching config from ${i}`);try{let c={};process.env.GITHUB_TOKEN&&(c.Authorization=`token ${process.env.GITHUB_TOKEN}`);let r=await fetch(i,{signal:AbortSignal.timeout(1e4),headers:c});if(!r.ok)return o.debug(`No .gibil.yml found in repo (${r.status})`),null;let l=await r.text();return De(l)}catch{return o.debug("Failed to fetch repo config, continuing without it"),null}}function Ve(t){t.command("create").description("Spin up one or more ephemeral dev machines").option("-n, --name <name>","Instance name").option("-f, --fleet <count>","Number of instances to create in parallel").option("-r, --repo <git-url>","Git repository to clone on startup").option("--json","Output instance info as JSON").option("--ttl <minutes>","Auto-destroy after N minutes","60").option("-c, --config <path>","Path to .gibil.yml config").option("--server-type <type>","Hetzner server type (e.g. cpx11, cpx21)").option("--location <loc>","Hetzner location (e.g. fsn1, nbg1)").option("-q, --quiet","Suppress non-essential output").option("-e, --env <KEY=VALUE...>","Environment variables to set on the server").action(async e=>{e.json&&$(!0);let n=te(e.ttl??"60","TTL");if(n>10080)throw new Error("TTL cannot exceed 7 days (10080 minutes).");let s=te(e.fleet??"1","Fleet count");if(s>20)throw new Error("Fleet size cannot exceed 20. Contact support for higher limits.");e.name&&Je(e.name);let i={};if(e.env)for(let a of e.env){let u=a.indexOf("=");if(u<=0)throw new Error(`Invalid --env format: "${a}". Use KEY=VALUE.`);i[a.slice(0,u)]=a.slice(u+1)}i.GITHUB_TOKEN&&!process.env.GITHUB_TOKEN&&(process.env.GITHUB_TOKEN=i.GITHUB_TOKEN);let c=null;e.config?c=await le(e.config):e.repo?c=await Zt(e.repo)??await le(process.cwd()):c=await le(process.cwd()),Object.keys(i).length>0&&(c||(c={}),c.env={...c.env,...i});let r=await A();if(r){o.info("Verifying API key...");let a=await M(r);o.info(` Authenticated as ${a.user.email} (${a.user.plan})`)}let l=await P.create();if(s===1){let a=e.name??pe(),u=Date.now(),f=o.spin(`Forging "${a}"...`),g=await We(l,a,{repo:e.repo,ttlMinutes:n,config:c,serverType:e.serverType,location:e.location}),d=((Date.now()-u)/1e3).toFixed(1);f.succeed(x.createReady(a,d)),r&&await W(r,"create",g.name,e.serverType).catch(m=>o.debug(`Usage tracking failed: ${m instanceof Error?m.message:String(m)}`)),e.json?o.json(Ye(g)):(o.info(""),o.info(je("Server ready",[`${p("Name:")} ${y(g.name)}`,`${p("IP:")} ${g.ip}`,`${p("TTL:")} ${n} minutes`,`${p("SSH:")} ${y(`gibil ssh ${g.name}`)}`])),o.info(""))}else{let a=qe(),u=e.name??"gibil",f=Date.now(),g=o.spin(`Forging fleet "${a}" \u2014 ${s} servers...`),d=Array.from({length:s},(b,k)=>`${u}-${k+1}-${a.slice(6)}`),m=await Promise.allSettled(d.map(b=>We(l,b,{repo:e.repo,ttlMinutes:n,config:c,serverType:e.serverType,location:e.location,fleetId:a}))),v=[],S=[];for(let b=0;b<m.length;b++){let k=m[b];k.status==="fulfilled"?v.push(k.value):S.push(`${d[b]}: ${k.reason instanceof Error?k.reason.message:String(k.reason)}`)}let O=((Date.now()-f)/1e3).toFixed(1);if(g.succeed(x.fleetReady(v.length,s)+` ${p(`(${O}s)`)}`),r&&await Promise.all(v.map(b=>W(r,"create",b.name,e.serverType).catch(k=>o.debug(`Usage tracking failed for ${b.name}: ${k instanceof Error?k.message:String(k)}`)))),e.json)o.json({fleet_id:a,instances:v.map(Ye),errors:S});else{o.info("");for(let b of v)o.info(` ${H} ${y(b.name)} ${p("\u2192")} ${b.ip}`);for(let b of S)o.info(` ${Z} ${b}`);o.info("")}}})}import{spawn as Xt}from"child_process";function Ze(t){t.command("ssh <name>").description("SSH into a running ephemeral machine").action(async e=>{let n=await E(e),s=["-A","-i",n.keyPath,"-o","StrictHostKeyChecking=no","-o","UserKnownHostsFile=/dev/null","-o","LogLevel=ERROR",`root@${n.ip}`];Xt("ssh",s,{stdio:"inherit"}).on("exit",c=>{process.exit(c??0)})})}function Xe(t){t.command("run <name> <command...>").description("Execute a command on a running instance").option("--json","Output result as JSON").option("--timeout <seconds>","Command timeout in seconds (default: 30)").action(async(e,n,s)=>{s.json&&$(!0);let i=await E(e),c=n.join(" "),r=s.timeout?te(s.timeout,"Timeout")*1e3:3e4;o.info(`Running on "${e}" (${i.ip}): ${c}`);let l=await C({instanceName:e,ip:i.ip,command:c,stream:!s.json,timeoutMs:r});s.json?o.json({instance:e,command:c,stdout:l.stdout,stderr:l.stderr,exit_code:l.exitCode}):l.exitCode!==0&&o.error(`Command exited with code ${l.exitCode}`),process.exit(l.exitCode??1)})}D();async function Qe(t,e){let n=await Fe(e);o.info(`Destroying instance "${e}" (server ${n.serverId})...`);try{await t.destroyServer(n.serverId)}catch(i){o.warn(`Could not delete server ${n.serverId}: ${i instanceof Error?i.message:String(i)}`)}try{await t.deleteSSHKey(n.sshKeyId)}catch(i){o.warn(`Could not delete SSH key ${n.sshKeyId}: ${i instanceof Error?i.message:String(i)}`)}await Y(e),await ue(e);let s=await A();s&&await W(s,"destroy",e).catch(i=>o.warn(`Usage tracking failed (billing may be inaccurate): ${i instanceof Error?i.message:String(i)}`)),o.info(` ${H} ${x.destroySingle(e)}`)}function et(t){t.command("destroy [name]").description("Destroy a running ephemeral machine").option("-a, --all","Destroy all gibil instances").option("--json","Output result as JSON").action(async(e,n)=>{if(n.json&&$(!0),n.all){let s=await B();if(s.length===0){n.json?o.json({destroyed:[],failed:[]}):o.info(x.noInstances);return}let i=await P.create();o.info(`Destroying ${s.length} instance(s)...`);let c=await Promise.allSettled(s.map(a=>Qe(i,a.name))),r=[],l=[];for(let a=0;a<c.length;a++)if(c[a].status==="fulfilled")r.push(s[a].name);else{let u=c[a].reason;l.push(`${s[a].name}: ${u instanceof Error?u.message:String(u)}`)}n.json?o.json({destroyed:r,failed:l}):l.length===0?o.info(`
|
|
16
|
-
${x.
|
|
17
|
-
${
|
|
18
|
-
${
|
|
19
|
-
`)||"(no output)"}],isError:u.exitCode!==0}}),s.tool("vm_read","Read a file from a remote server. Returns the file contents with line numbers.",{path:h.string().describe("Absolute path on the server (e.g. /root/project/src/app.ts)"),offset:h.number().optional().describe("Start at line N (1-based)"),limit:h.number().optional().describe("Max lines to return"),server:i},async r=>{let l=await ne(e,r.server),a=j(r.path),u=`cat -n ${a}`;r.offset&&r.limit?u=`sed -n '${r.offset},${r.offset+r.limit-1}p' ${a} | cat -n`:r.offset?u=`tail -n +${r.offset} ${a} | cat -n`:r.limit&&(u=`head -n ${r.limit} ${a} | cat -n`);let f=await q(l,u);return f.exitCode!==0?{content:[{type:"text",text:`Error: ${f.stderr}`}],isError:!0}:{content:[{type:"text",text:f.stdout}]}}),s.tool("vm_write","Write content to a file on a remote server. Creates parent directories if needed. Overwrites existing files.",{path:h.string().describe("Absolute path on the server"),content:h.string().describe("File content to write"),server:i},async r=>{let l=await ne(e,r.server),a=Buffer.from(r.content).toString("base64"),u=j(r.path),f=`mkdir -p "$(dirname ${u})" && echo '${a}' | base64 -d > ${u}`,g=await q(l,f);return g.exitCode!==0?{content:[{type:"text",text:`Error: ${g.stderr}`}],isError:!0}:{content:[{type:"text",text:`Wrote ${r.path}`}]}}),s.tool("vm_ls","List files and directories on a remote server.",{path:h.string().optional().describe("Directory path (default: /root/project)"),glob:h.string().optional().describe("Glob pattern to filter (e.g. '**/*.ts')"),server:i},async r=>{let l=await ne(e,r.server),a=r.path??"/root/project",u;r.glob?u=`cd ${j(a)} && find . -path './${r.glob}' -type f 2>/dev/null | sort | head -200`:u=`ls -la ${j(a)}`;let f=await q(l,u);return f.exitCode!==0?{content:[{type:"text",text:`Error: ${f.stderr}`}],isError:!0}:{content:[{type:"text",text:f.stdout}]}}),s.tool("vm_grep","Search for a pattern in files on a remote server. Uses ripgrep if available, falls back to grep.",{pattern:h.string().describe("Regex pattern to search for"),path:h.string().optional().describe("Directory or file to search (default: /root/project)"),include:h.string().optional().describe("File glob to include (e.g. '*.ts')"),server:i},async r=>{let l=await ne(e,r.server),a=r.path??"/root/project",u=j(r.pattern),f=j(a),g;if(r.include){let v=j(r.include);g=`cd ${f} && (rg -n --glob ${v} ${u} 2>/dev/null || grep -rn --include=${v} ${u} .) | head -100`}else g=`cd ${f} && (rg -n ${u} 2>/dev/null || grep -rn ${u} .) | head -100`;return{content:[{type:"text",text:(await q(l,g)).stdout||"(no matches)"}]}});let c=new on;await s.connect(c)}function lt(t){t.command("mcp [name]").description("Start an MCP server (used by Claude Code, Cursor, and other agents)").action(async e=>{try{await ct(e)}catch(n){o.error(n instanceof Error?n.message:String(n)),process.exit(1)}})}D();import{createInterface as sn}from"readline";import{execSync as an}from"child_process";import{existsSync as cn}from"fs";import{join as ln}from"path";R();function ke(t){let e=sn({input:process.stdin,output:process.stderr});return new Promise(n=>{e.question(t,s=>{e.close(),n(s.trim())})})}async function un(){let t=!!await ve(),e=!!await A();return{hetzner:t,apiKey:e}}function ut(t){t.command("init").description("Set up gibil \u2014 configure your forge in 60 seconds").option("--force","Reconfigure even if already set up").action(async e=>{console.error(Ae);let n=await un();if(n.hetzner&&!e.force){o.info(`${H} Already configured.`),n.apiKey?(o.detail("Hetzner",z("connected")),o.detail("Gibil API",z("connected"))):(o.detail("Hetzner",z("connected")),o.detail("Gibil API",p("not configured (optional)"))),o.info(""),o.info(` Run ${y("gibil init --force")} to reconfigure.`),o.info(` Run ${y("gibil create")} to forge a server.`);return}o.info(""),o.info(y("Step 1: Hetzner API Token")),o.info(p(" Your servers run on Hetzner Cloud. You need an API token.")),o.info(p(" Get one at: https://console.hetzner.cloud \u2192 API Tokens")),o.info("");let s=await ke(" Hetzner API token: ");s||(o.error("No token provided. Run gibil init again when ready."),process.exit(1));let i=o.spin("Verifying Hetzner token...");try{let d=await(await fetch("https://api.hetzner.cloud/v1/servers",{headers:{Authorization:`Bearer ${s}`}})).json();d.error&&(i.fail(`Invalid token: ${d.error.message}`),process.exit(1)),i.succeed("Hetzner token verified")}catch{i.fail("Could not reach Hetzner API. Check your network."),process.exit(1)}await Q(s);let c=o.spin("Detecting available server types..."),r="cax11",l="fsn1",a=[{type:"cax11",location:"fsn1"},{type:"cpx11",location:"fsn1"}];for(let g of a)try{let m=await(await fetch("https://api.hetzner.cloud/v1/servers",{method:"POST",headers:{Authorization:`Bearer ${s}`,"Content-Type":"application/json"},body:JSON.stringify({name:"gibil-probe",server_type:g.type,image:"ubuntu-24.04",location:g.location,start_after_create:!1})})).json();if(m.server){await fetch(`https://api.hetzner.cloud/v1/servers/${m.server.id}`,{method:"DELETE",headers:{Authorization:`Bearer ${s}`}}),r=g.type,l=g.location;break}}catch{}await we(r,l),c.succeed(`Default server type: ${r} (${l})`),o.info(""),o.info(y("Step 2: Gibil API Key")+p(" (optional)")),o.info(p(" For usage tracking and plan limits.")),o.info(p(" Skip this if you just want to use your own Hetzner token.")),o.info("");let u=await ke(" Gibil API key (or press Enter to skip): ");if(u)if(!u.startsWith("pk_"))o.warn('API keys start with "pk_". Skipping.');else{let g=o.spin("Verifying API key...");try{let d=await M(u);await X(u),g.succeed(`Logged in as ${d.user.email} (${d.user.plan})`)}catch(d){g.fail(`Could not verify key: ${d instanceof Error?d.message:String(d)}`),o.info(p(" Continuing without Gibil API. You can add it later with: gibil auth login"))}}else o.info(p(" Skipped. You can add it later with: gibil auth login"));if(o.info(""),o.info(x.initComplete),o.info(""),o.info(p(" Quick start:")),o.info(` ${y("gibil create")} ${p("Forge a server")}`),o.info(` ${y("gibil create --repo github.com/you/project")} ${p("Clone a repo on boot")}`),o.info(` ${y("gibil ssh <name>")} ${p("Connect to it")}`),o.info(` ${y("gibil destroy <name>")} ${p("Burn it down")}`),o.info(""),(await ke(" Install the gibil agent skill? (Y/n): ")).toLowerCase()!=="n"){let g=o.spin("Installing gibil skill...");try{an("npx -y skills add https://github.com/AlexikM/gibil-skills --skill gibil -y -g",{stdio:"pipe",timeout:3e4}),g.succeed("Gibil skill installed for your AI agents")}catch{g.fail("Could not install skill automatically"),o.info(p(" Install manually: npx skills add https://github.com/AlexikM/gibil-skills --skill gibil"))}}else o.info(p(" Skipped. Install later: npx skills add https://github.com/AlexikM/gibil-skills --skill gibil"))})}async function pt(){if(process.env.HETZNER_API_TOKEN)return!1;let t=ln(w.root,"config.json");return!cn(t)}try{await import("dotenv/config")}catch{}var hn=fn(gn(import.meta.url)),dt={version:"0.0.0"};for(let t of["../package.json","../../package.json"])try{dt=JSON.parse(dn(mn(hn,t),"utf-8"));break}catch{}var _=new pn;_.name("gibil").description("Ephemeral dev compute for humans and AI agents").version(`${dt.version} ${re}`,"-v, --version").addHelpText("before",`
|
|
20
|
-
|
|
8
|
+
${v(" g i b i l")} ${p(ut)}
|
|
9
|
+
`,Vt=`${E} ${v("gibil")} ${p(ut)}`,qt=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],et=class{timer=null;frame=0;text;constructor(t){this.text=t}start(){return process.stderr.isTTY?(this.timer=setInterval(()=>{let t=R(qt[this.frame%qt.length]);process.stderr.write(`\r ${t} ${this.text}`),this.frame++},80),this):(process.stderr.write(` ${this.text}
|
|
10
|
+
`),this)}update(t){this.text=t,process.stderr.isTTY||process.stderr.write(` ${t}
|
|
11
|
+
`)}succeed(t){this.stop(),process.stderr.write(`\r ${H} ${t??this.text}
|
|
12
|
+
`)}fail(t){this.stop(),process.stderr.write(`\r ${nt} ${t??this.text}
|
|
13
|
+
`)}stop(){this.timer&&(clearInterval(this.timer),this.timer=null,process.stderr.isTTY&&process.stderr.write("\r\x1B[K"))}};function Zt(e,t){let n=Math.max(e.length+4,...t.map(a=>Et(a).length+4)),r=`${p("\u256D")}${p("\u2500".repeat(n))}${p("\u256E")}`,o=`${p("\u2570")}${p("\u2500".repeat(n))}${p("\u256F")}`,c=`${p("\u2502")} ${E} ${v(e)}${" ".repeat(n-Et(e).length-4)}${p("\u2502")}`,i=`${p("\u251C")}${p("\u2500".repeat(n))}${p("\u2524")}`,l=t.map(a=>{let u=n-Et(a).length-2;return`${p("\u2502")} ${a}${" ".repeat(Math.max(0,u))}${p("\u2502")}`});return[r,c,i,...l,o].join(`
|
|
14
|
+
`)}function Et(e){return e.replace(/\x1b\[[0-9;]*m/g,"")}var k={welcome:`${E} Your first fire. Welcome to Gibil.`,noInstances:`${E} No fires burning. Gibil sleeps.`,destroyAll:`${E} All fires extinguished. Gibil moves on.`,destroySingle:e=>`${E} "${e}" \u2014 fire out.`,authSuccess:`${E} Logged in. The forge is yours.`,authLogout:`${E} Logged out. The forge cools.`,createReady:(e,t)=>`${E} "${e}" forged ${p(`(${t}s)`)}`,fleetReady:(e,t)=>`${E} Fleet forged \u2014 ${e}/${t} fires lit.`,ttlWarning:(e,t)=>`${E} ${e} \u2014 flame is low (${t}m remaining)`,initComplete:`${E} The forge is ready. Run ${v("gibil create")} to light your first fire.`,setupNeeded:`${E} No forge configured. Run ${v("gibil init")} to get started.`};var Le="info",Pt=!1,Xt={debug:0,info:1,warn:2,error:3,silent:4};function $(e){Pt=e}function K(e){return Pt&&e!=="error"?!1:Xt[e]>=Xt[Le]}var s={debug(e,...t){K("debug")&&console.debug(`${p("[debug]")} ${e}`,...t)},info(e,...t){K("info")&&console.log(e,...t)},warn(e,...t){K("warn")&&console.warn(`${Wt} ${e}`,...t)},error(e,...t){K("error")&&console.error(`${nt} ${e}`,...t)},success(e){K("info")&&console.log(`${H} ${e}`)},step(e){K("info")&&console.log(` ${p("\u203A")} ${e}`)},flame(e){K("info")&&console.log(e)},detail(e,t){K("info")&&console.log(` ${p(e+":")} ${t}`)},spin(e){return Pt?new et(e):new et(e).start()},json(e){console.log(JSON.stringify(e,null,2))}};var Ye="https://api.hetzner.cloud/v1",T=class e{token;constructor(t){this.token=t}static async create(t){let{getHetznerToken:n}=await Promise.resolve().then(()=>(J(),Mt)),r=t??await n();if(!r)throw new Error("HETZNER_API_TOKEN is required. Run 'gibil init' or set it in your environment.");return new e(r)}async request(t,n,r){let o=`${Ye}${n}`;s.debug(`${t} ${o}`);let c=await fetch(o,{method:t,headers:{Authorization:`Bearer ${this.token}`,"Content-Type":"application/json"},body:r?JSON.stringify(r):void 0});if(!c.ok){let i=await c.text(),l;try{l=JSON.parse(i).error?.message??i}catch{l=i}throw new Error(`Hetzner API error (${c.status}): ${l}`)}return c.status===204?{}:await c.json()}async createServer(t,n,r,o,c){if(!o||!c){let{getServerDefaults:u}=await Promise.resolve().then(()=>(J(),Mt)),g=await u();o=o??g.serverType,c=c??g.location}if(o.startsWith("cax")&&!["fsn1","nbg1"].includes(c))throw new Error(`ARM server type "${o}" is not available in "${c}". Use --location fsn1 or --location nbg1, or switch to an x86 type (cpx11, cpx21, etc.).`);let a={name:t,server_type:o,image:"ubuntu-24.04",ssh_keys:[n],labels:{gibil:"true","gibil-name":t},location:c};s.debug(`createServer payload: ${JSON.stringify({name:t,server_type:o,image:"ubuntu-24.04",location:c})}`),r&&(a.user_data=r);try{return(await this.request("POST","/servers",a)).server}catch(u){let g=`(server_type=${o}, location=${c}). Try a different --server-type or --location.`;throw u instanceof Error?new Error(`${u.message} ${g}`):u}}async destroyServer(t){await this.request("DELETE",`/servers/${t}`)}async getServer(t){return(await this.request("GET",`/servers/${t}`)).server}async listServers(t="gibil=true"){return(await this.request("GET",`/servers?label_selector=${encodeURIComponent(t)}&per_page=50`)).servers}async waitForReady(t,n=12e4){let r=Date.now(),o=3e3;for(;Date.now()-r<n;){let c=await this.getServer(t);if(c.status==="running"&&c.public_net.ipv4.ip!=="0.0.0.0")return c;s.debug(`Server ${t} status: ${c.status}, waiting...`),await new Promise(i=>setTimeout(i,o))}throw new Error(`Server ${t} did not become ready within ${n/1e3}s`)}async createSSHKey(t,n){return(await this.request("POST","/ssh_keys",{name:t,public_key:n})).ssh_key}async deleteSSHKey(t){await this.request("DELETE",`/ssh_keys/${t}`)}};M();import{mkdir as Ve,rm as te,readFile as Ze,chmod as Xe}from"fs/promises";import{existsSync as ee}from"fs";import{execFile as Qe}from"child_process";import{promisify as tn}from"util";var en=tn(Qe);async function mt(e){let t=S.keyDir(e);ee(t)&&await te(t,{recursive:!0}),await Ve(t,{recursive:!0});let n=S.privateKey(e),r=S.publicKey(e);await en("ssh-keygen",["-t","ed25519","-f",n,"-N","","-C",`gibil-${e}`]),await Xe(n,384);let o=await Ze(r,"utf-8");return{privateKeyPath:n,publicKeyPath:r,publicKey:o.trim()}}async function Q(e){let t=S.keyDir(e);ee(t)&&await te(t,{recursive:!0})}M();import{Client as nn}from"ssh2";import{readFile as on}from"fs/promises";async function I(e){let{instanceName:t,ip:n,command:r,stream:o=!1,timeoutMs:c=3e4}=e,i=await on(S.privateKey(t),"utf-8");return new Promise((l,a)=>{let u=new nn,g="",f="";u.on("ready",()=>{s.debug(`SSH connected to ${n}`),u.exec(r,(d,m)=>{if(d)return u.end(),a(d);m.on("data",h=>{let b=h.toString();g+=b,o&&process.stdout.write(b)}),m.stderr.on("data",h=>{let b=h.toString();f+=b,o&&process.stderr.write(b)}),m.on("close",h=>{u.end(),l({stdout:g,stderr:f,exitCode:h??0})})})}).on("error",d=>{let m="";d.code==="ECONNREFUSED"?m=" (instance may have been destroyed or is still booting)":d.code==="EHOSTUNREACH"?m=" (IP unreachable \u2014 instance may not be running)":d.code==="ETIMEDOUT"&&(m=" (connection timed out \u2014 check if instance is running with 'gibil list')"),a(new Error(`SSH connection to ${n} failed: ${d.message}${m}`))}).connect({host:n,port:22,username:"root",privateKey:i,readyTimeout:c,hostVerifier:()=>!0,agent:process.env.SSH_AUTH_SOCK,agentForward:!0})})}async function gt(e,t,n=12e4){let r=Date.now(),o=5e3;for(;Date.now()-r<n;)try{await I({instanceName:e,ip:t,command:"echo ready",timeoutMs:1e4});return}catch{s.debug(`SSH not ready on ${t}, retrying...`),await new Promise(c=>setTimeout(c,o))}throw new Error(`SSH did not become available on ${t} within ${n/1e3}s`)}function ft(e){let{repo:t,config:n,ttlMinutes:r,githubToken:o,gitIdentity:c}=e,i=["#!/bin/bash","set -euo pipefail","","# \u2500\u2500 Gibil cloud-init \u2500\u2500","export HOME=/root","export DEBIAN_FRONTEND=noninteractive"];o&&i.push(`export GITHUB_TOKEN=${A(o)}`),i.push("","# Base packages","apt-get update -qq","apt-get install -y -qq git curl wget build-essential unzip > /dev/null 2>&1","","# Install GitHub CLI","if ! type gh > /dev/null 2>&1; then"," curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg 2>/dev/null"," chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg"," ARCH=$(dpkg --print-architecture)",' echo "deb [arch=${ARCH} signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" > /etc/apt/sources.list.d/github-cli.list'," apt-get update -qq && apt-get install -y -qq gh > /dev/null 2>&1","fi","");let l=n?.image??"node:20";if(i.push(...rn(l)),n?.services&&n.services.length>0){i.push(...sn()),i.push("");for(let a of n.services)i.push(...an(a))}if(n?.env){i.push("# Environment variables");for(let[a,u]of Object.entries(n.env))i.push(`export ${a}=${A(u)}`),i.push(`echo ${A(`${a}=${u}`)} >> /etc/environment`);i.push("")}if(i.push("# Configure git"),c?(i.push(`git config --global user.email ${A(c.email)}`),i.push(`git config --global user.name ${A(c.name)}`),c.signingKey&&(i.push("git config --global gpg.format ssh"),i.push(`git config --global user.signingkey ${A("key::"+c.signingKey)}`),i.push("git config --global commit.gpgsign true"),i.push("git config --global tag.gpgsign true"),i.push("mkdir -p /root/.ssh"),i.push(`echo ${A(c.email+" "+c.signingKey)} > /root/.ssh/allowed_signers`),i.push("git config --global gpg.ssh.allowedSignersFile /root/.ssh/allowed_signers"))):(i.push("git config --global user.email 'gibil@bot.dev'"),i.push("git config --global user.name 'Gibil Bot'")),i.push(""),t){let a=t.match(/github\.com\/([^/]+\/[^/.]+)/);if(i.push("# Clone repository"),i.push("cd /root"),a){let u=a[1];i.push('if [ -n "${GITHUB_TOKEN:-}" ]; then'),i.push(` CLONE_URL="https://x-access-token:\${GITHUB_TOKEN}@github.com/${u}.git"`),i.push("else"),i.push(` CLONE_URL='https://github.com/${u}.git'`),i.push("fi"),i.push('timeout 300 git clone "$CLONE_URL" /root/project || { echo "Git clone failed or timed out"; exit 1; }')}else i.push(`timeout 300 git clone ${A(t)} /root/project || { echo "Git clone failed or timed out"; exit 1; }`);i.push("cd /root/project"),i.push(""),i.push('if [ -n "${GITHUB_TOKEN:-}" ]; then'),i.push(' echo "${GITHUB_TOKEN}" | gh auth login --with-token 2>/dev/null || true'),a&&i.push(` git -C /root/project remote set-url origin "https://x-access-token:\${GITHUB_TOKEN}@github.com/${a[1]}.git"`),i.push("fi"),i.push("")}if(r&&r>0&&(i.push("# Auto-destroy after TTL"),i.push(`echo "shutdown -h now" | at now + ${r} minutes 2>/dev/null || true`),i.push(`(sleep ${r*60} && shutdown -h now) &`),i.push("")),i.push("# Clean up cloud-init secrets"),i.push("rm -f /var/lib/cloud/instance/user-data.txt"),i.push(""),i.push("# Signal that infrastructure is ready"),i.push("touch /root/.gibil-ready"),i.push('echo "Gibil infrastructure ready"'),i.push(""),t&&n?.tasks&&n.tasks.length>0){i.push("# Run project tasks"),i.push("cd /root/project");for(let a of n.tasks)i.push(`echo '\u25B6 Running task: '${A(a.name)}`),i.push(`if ! ${a.command}; then`),i.push(` echo '\u2717 Task failed: '${A(a.name)}`),i.push(" touch /root/.gibil-tasks-failed"),i.push("fi");i.push(""),i.push("# Signal tasks complete"),i.push("if [ ! -f /root/.gibil-tasks-failed ]; then"),i.push(" touch /root/.gibil-tasks-done"),i.push(' echo "Gibil tasks complete"'),i.push("else"),i.push(' echo "Gibil tasks finished with errors"'),i.push("fi")}return i.join(`
|
|
15
|
+
`)}function rn(e){let t=[];if(e.startsWith("node:")){let n=e.split(":")[1]??"20";t.push("# Install Node.js"),t.push(`curl -fsSL https://deb.nodesource.com/setup_${n}.x | bash - > /dev/null 2>&1`),t.push("apt-get install -y -qq nodejs > /dev/null 2>&1"),t.push("npm install -g pnpm@latest > /dev/null 2>&1"),t.push("")}else if(e.startsWith("python:")){let n=e.split(":")[1]??"3.12";t.push("# Install Python"),t.push(`apt-get install -y -qq python${n} python3-pip python3-venv > /dev/null 2>&1`),t.push("")}else if(e.startsWith("go:")){let n=e.split(":")[1]??"1.22";t.push("# Install Go"),t.push('GO_ARCH=$(uname -m | sed "s/x86_64/amd64/" | sed "s/aarch64/arm64/")'),t.push(`wget -q https://go.dev/dl/go${n}.linux-\${GO_ARCH}.tar.gz -O /tmp/go.tar.gz`),t.push("tar -C /usr/local -xzf /tmp/go.tar.gz"),t.push("export PATH=$PATH:/usr/local/go/bin"),t.push('echo "export PATH=\\$PATH:/usr/local/go/bin" >> /root/.bashrc'),t.push("")}else t.push("# Install Node.js (default)"),t.push("curl -fsSL https://deb.nodesource.com/setup_20.x | bash - > /dev/null 2>&1"),t.push("apt-get install -y -qq nodejs > /dev/null 2>&1"),t.push("npm install -g pnpm@latest > /dev/null 2>&1"),t.push("");return t}function sn(){return["# Install Docker","curl -fsSL https://get.docker.com | sh > /dev/null 2>&1","systemctl enable docker --now"]}function an(e){let t=[];t.push(`# Start service: ${e.name.replace(/[^a-zA-Z0-9_-]/g,"")}`);let r=`docker run -d --name ${e.name.replace(/[^a-zA-Z0-9_-]/g,"")}`;if(e.port&&(r+=` -p ${e.port}:${e.port}`),e.env)for(let[o,c]of Object.entries(e.env))r+=` -e ${o}=${A(c)}`;return r+=` ${e.image}`,t.push(r),t.push(""),t}function A(e){return`'${e.replace(/'/g,"'\\''")}'`}import{readFile as cn}from"fs/promises";import{existsSync as ne,statSync as ln}from"fs";import{join as un}from"path";import{parse as re}from"yaml";var dn=".gibil.yml";async function ht(e){let t;if(ne(e)&&ln(e).isFile()?t=e:t=un(e,dn),!ne(t))return null;let n=await cn(t,"utf-8"),r=re(n);return se(r)}function ie(e){let t=re(e);return se(t)}function se(e){if(!e||typeof e!="object")throw new Error("Invalid .gibil.yml: must be a YAML object");let t=e,n={};return typeof t.name=="string"&&(n.name=t.name),typeof t.image=="string"&&(n.image=t.image),typeof t.server_type=="string"&&(n.server_type=t.server_type),typeof t.location=="string"&&(n.location=t.location),Array.isArray(t.services)&&(n.services=t.services.map(r=>{let o=r;if(typeof o.name!="string"||typeof o.image!="string")throw new Error("Each service must have a 'name' and 'image' field");return{name:o.name,image:o.image,port:typeof o.port=="number"?o.port:void 0,env:oe(o.env,`service "${o.name}"`)}})),Array.isArray(t.tasks)&&(n.tasks=t.tasks.map(r=>{let o=r;if(typeof o.name!="string"||typeof o.command!="string")throw new Error("Each task must have a 'name' and 'command' field");return{name:o.name,command:o.command}})),t.env!==void 0&&(n.env=oe(t.env,"top-level")),n}function oe(e,t){if(e==null)return;if(typeof e!="object"||Array.isArray(e))throw new Error(`env in ${t} must be a key-value object`);let n={};for(let[r,o]of Object.entries(e))if(typeof o=="string")n[r]=o;else if(typeof o=="number"||typeof o=="boolean")n[r]=String(o);else throw new Error(`env.${r} in ${t} must be a string, number, or boolean \u2014 got ${typeof o}`);return Object.keys(n).length>0?n:void 0}M();import{readFile as pn,writeFile as mn,mkdir as ae,rm as gn,readdir as fn}from"fs/promises";import{existsSync as ce}from"fs";import{join as Rt}from"path";var Kt=class{instancesDir;keysDir;constructor(t){let n=t??S.root;this.instancesDir=Rt(n,"instances"),this.keysDir=Rt(n,"keys")}async ensureDirectories(){await ae(this.instancesDir,{recursive:!0,mode:448}),await ae(this.keysDir,{recursive:!0,mode:448})}instanceFile(t){return Rt(this.instancesDir,`${t}.json`)}async save(t){await this.ensureDirectories(),await mn(this.instanceFile(t.name),JSON.stringify(t,null,2),{mode:384})}async load(t){let n=this.instanceFile(t);if(!ce(n))return null;let r=await pn(n,"utf-8");return JSON.parse(r)}async loadOrThrow(t){let n=await this.load(t);if(!n)throw new Error(`Instance "${t}" not found. Run "gibil list" to see active instances.`);return n}async loadActiveOrThrow(t){let n=await this.loadOrThrow(t);if(new Date>new Date(n.expiresAt))throw new Error(`Instance "${t}" has expired (TTL was ${n.ttlMinutes}m). Run "gibil destroy ${t}" to clean up.`);return n}async delete(t){let n=this.instanceFile(t);ce(n)&&await gn(n)}async list(){await this.ensureDirectories();let t=await fn(this.instancesDir),n=[];for(let r of t){if(!r.endsWith(".json"))continue;let o=r.replace(".json",""),c=await this.load(o);c&&n.push(c)}return n}},it=new Kt;var W=e=>it.save(e);var yt=e=>it.loadOrThrow(e),_=e=>it.loadActiveOrThrow(e),bt=e=>it.delete(e),Y=()=>it.list();import{randomBytes as hn}from"crypto";function Dt(e=6){return hn(Math.ceil(e/2)).toString("hex").slice(0,e)}function wt(){return`gibil-${Dt()}`}function le(){return`fleet-${Dt(8)}`}function vt(){return`j-${Dt(8)}`}M();var yn=/^[a-zA-Z0-9][a-zA-Z0-9_-]{0,62}$/;function ue(e){if(!yn.test(e))throw new Error(`Invalid instance name "${e}". Names must be 1-63 chars, start with alphanumeric, and contain only [a-zA-Z0-9_-].`);return e}function st(e,t){let n=parseInt(e,10);if(isNaN(n)||n<=0)throw new Error(`${t} must be a positive integer, got "${e}"`);return n}J();import{execSync as $t}from"child_process";import{readFileSync as bn}from"fs";function wn(){try{let e=$t("git config user.name",{encoding:"utf-8"}).trim(),t=$t("git config user.email",{encoding:"utf-8"}).trim();if(!e||!t)return;let n;try{if($t("git config gpg.format",{encoding:"utf-8"}).trim()==="ssh"){let o=$t("git config user.signingkey",{encoding:"utf-8"}).trim();if(o)try{n=bn(o,"utf-8").trim()}catch{(o.startsWith("ssh-")||o.startsWith("key::"))&&(n=o.replace(/^key::/,""))}}}catch{}return{name:e,email:t,signingKey:n}}catch{return}}async function de(e,t,n){s.step("Generating SSH keys...");let r=await mt(t),o;try{s.step("Uploading SSH key..."),o=await e.createSSHKey(`gibil-${t}`,r.publicKey),n.repo&&n.repo.includes("github.com")&&!process.env.GITHUB_TOKEN&&s.debug("No GITHUB_TOKEN set \u2014 private repos will fail to clone. Set GITHUB_TOKEN to enable private repo access.");let c=wn(),i=ft({repo:n.repo,config:n.config??void 0,ttlMinutes:n.ttlMinutes,githubToken:process.env.GITHUB_TOKEN,gitIdentity:c});s.step("Creating server...");let l=await e.createServer(t,o.id,i,n.serverType??n.config?.server_type,n.location??n.config?.location);s.step("Waiting for server...");let u=(await e.waitForReady(l.id)).public_net.ipv4.ip,g=new Date,f={name:t,serverId:l.id,ip:u,sshKeyId:o.id,keyPath:S.privateKey(t),status:"running",createdAt:g.toISOString(),ttlMinutes:n.ttlMinutes,expiresAt:new Date(g.getTime()+n.ttlMinutes*6e4).toISOString(),repo:n.repo,fleetId:n.fleetId,gitIdentity:c};if(await W(f),s.step(`Waiting for SSH on ${u}...`),await gt(t,u),n.repo||n.config){s.step("Waiting for provisioning...");let d=36e4,m=5e3,h=Date.now(),b=!1;for(;Date.now()-h<d;){try{if((await I({instanceName:t,ip:u,command:"test -f /root/.gibil-ready && echo ready || echo waiting",timeoutMs:1e4})).stdout.trim()==="ready"){b=!0;break}}catch{}await new Promise(C=>setTimeout(C,m))}if(!b)try{let C=await I({instanceName:t,ip:u,command:"tail -20 /var/log/cloud-init-output.log 2>/dev/null || echo 'No cloud-init log found'",timeoutMs:1e4});s.warn("Provisioning may have failed. Cloud-init log:"),s.info(C.stdout)}catch{s.warn("Provisioning may have failed \u2014 could not read cloud-init log.")}}return f}catch(c){if(s.error(`Failed to create instance "${t}", cleaning up...`),await Q(t).catch(i=>s.warn(`Could not clean up SSH keys: ${i instanceof Error?i.message:String(i)}`)),o){let i=o.id;await e.deleteSSHKey(i).catch(l=>s.warn(`Could not delete Hetzner SSH key ${i}: ${l instanceof Error?l.message:String(l)}`))}throw c}}function pe(e){let t=Math.max(0,Math.floor((new Date(e.expiresAt).getTime()-Date.now())/1e3));return{name:e.name,ip:e.ip,ssh:`ssh -i ${e.keyPath} -o StrictHostKeyChecking=no root@${e.ip}`,status:e.status,ttl_remaining:t,created_at:e.createdAt,fleet_id:e.fleetId}}async function vn(e){let t=e.match(/github\.com\/([^/]+)\/([^/.]+)/);if(!t)return s.debug(`Cannot fetch config from non-GitHub repo: ${e}`),null;let[,n,r]=t,o=`https://raw.githubusercontent.com/${n}/${r}/HEAD/.gibil.yml`;s.debug(`Fetching config from ${o}`);try{let c={};process.env.GITHUB_TOKEN&&(c.Authorization=`token ${process.env.GITHUB_TOKEN}`);let i=await fetch(o,{signal:AbortSignal.timeout(1e4),headers:c});if(!i.ok)return s.debug(`No .gibil.yml found in repo (${i.status})`),null;let l=await i.text();return ie(l)}catch{return s.debug("Failed to fetch repo config, continuing without it"),null}}function me(e){e.command("create").description("Spin up one or more ephemeral dev machines").option("-n, --name <name>","Instance name").option("-f, --fleet <count>","Number of instances to create in parallel").option("-r, --repo <git-url>","Git repository to clone on startup").option("--json","Output instance info as JSON").option("--ttl <minutes>","Auto-destroy after N minutes","60").option("-c, --config <path>","Path to .gibil.yml config").option("--server-type <type>","Hetzner server type (e.g. cpx11, cpx21)").option("--location <loc>","Hetzner location (e.g. fsn1, nbg1)").option("-q, --quiet","Suppress non-essential output").option("-e, --env <KEY=VALUE...>","Environment variables to set on the server").action(async t=>{t.json&&$(!0);let n=st(t.ttl??"60","TTL");if(n>10080)throw new Error("TTL cannot exceed 7 days (10080 minutes).");let r=st(t.fleet??"1","Fleet count");if(r>20)throw new Error("Fleet size cannot exceed 20. Contact support for higher limits.");t.name&&ue(t.name);let o={};if(t.env)for(let a of t.env){let u=a.indexOf("=");if(u<=0)throw new Error(`Invalid --env format: "${a}". Use KEY=VALUE.`);o[a.slice(0,u)]=a.slice(u+1)}o.GITHUB_TOKEN&&!process.env.GITHUB_TOKEN&&(process.env.GITHUB_TOKEN=o.GITHUB_TOKEN);let c=null;t.config?c=await ht(t.config):t.repo?c=await vn(t.repo)??await ht(process.cwd()):c=await ht(process.cwd()),Object.keys(o).length>0&&(c||(c={}),c.env={...c.env,...o});let i=await P();if(i){s.info("Verifying API key...");let a=await F(i);s.info(` Authenticated as ${a.user.email} (${a.user.plan})`)}let l=await T.create();if(r===1){let a=t.name??wt(),u=Date.now(),g=s.spin(`Forging "${a}"...`),f=await de(l,a,{repo:t.repo,ttlMinutes:n,config:c,serverType:t.serverType,location:t.location}),d=((Date.now()-u)/1e3).toFixed(1);g.succeed(k.createReady(a,d)),i&&await X(i,"create",f.name,t.serverType).catch(m=>s.debug(`Usage tracking failed: ${m instanceof Error?m.message:String(m)}`)),t.json?s.json(pe(f)):(s.info(""),s.info(Zt("Server ready",[`${p("Name:")} ${v(f.name)}`,`${p("IP:")} ${f.ip}`,`${p("TTL:")} ${n} minutes`,`${p("SSH:")} ${v(`gibil ssh ${f.name}`)}`])),s.info(""))}else{let a=le(),u=t.name??"gibil",g=Date.now(),f=s.spin(`Forging fleet "${a}" \u2014 ${r} servers...`),d=Array.from({length:r},(w,x)=>`${u}-${x+1}-${a.slice(6)}`),m=await Promise.allSettled(d.map(w=>de(l,w,{repo:t.repo,ttlMinutes:n,config:c,serverType:t.serverType,location:t.location,fleetId:a}))),h=[],b=[];for(let w=0;w<m.length;w++){let x=m[w];x.status==="fulfilled"?h.push(x.value):b.push(`${d[w]}: ${x.reason instanceof Error?x.reason.message:String(x.reason)}`)}let C=((Date.now()-g)/1e3).toFixed(1);if(f.succeed(k.fleetReady(h.length,r)+` ${p(`(${C}s)`)}`),i&&await Promise.all(h.map(w=>X(i,"create",w.name,t.serverType).catch(x=>s.debug(`Usage tracking failed for ${w.name}: ${x instanceof Error?x.message:String(x)}`)))),t.json)s.json({fleet_id:a,instances:h.map(pe),errors:b});else{s.info("");for(let w of h)s.info(` ${H} ${v(w.name)} ${p("\u2192")} ${w.ip}`);for(let w of b)s.info(` ${nt} ${w}`);s.info("")}}})}import{spawn as $n}from"child_process";function ge(e){e.command("ssh <name>").description("SSH into a running ephemeral machine").action(async t=>{let n=await _(t),r=["-A","-i",n.keyPath,"-o","StrictHostKeyChecking=no","-o","UserKnownHostsFile=/dev/null","-o","LogLevel=ERROR",`root@${n.ip}`];$n("ssh",r,{stdio:"inherit"}).on("exit",c=>{process.exit(c??0)})})}tt();function we(e){e.command("run <name> <command...>").description("Execute a command on a running instance").option("--json","Output result as JSON").option("--timeout <seconds>","Command timeout in seconds (default: 30)").option("-b, --background","Run in background, return job ID immediately").action(async(t,n,r)=>{r.json&&$(!0);let o=await _(t),c=n.join(" "),i=r.timeout?st(r.timeout,"Timeout")*1e3:3e4;if(r.background){let a=vt(),u="/root/.gibil-jobs",g=`${u}/${a}.log`,f=`${u}/${a}.exit`,d=`${u}/${a}.pid`,m=`${u}/${a}.sh`,h=["#!/bin/bash",`nohup bash -c '${c.replace(/'/g,"'\\''")}' > ${g} 2>&1 &`,"BGPID=$!",`echo $BGPID > ${d}`,`(wait $BGPID 2>/dev/null; echo $? > ${f}) &`,"echo $BGPID"].join(`
|
|
16
|
+
`),b=Buffer.from(h).toString("base64"),C=`mkdir -p ${u} && echo '${b}' | base64 -d > ${m} && chmod +x ${m} && bash ${m}`,w=await I({instanceName:t,ip:o.ip,command:C,timeoutMs:1e4}),x=parseInt(w.stdout.trim(),10);isNaN(x)&&(s.error("Failed to start background job \u2014 could not capture PID"),process.exit(1)),await z({id:a,instance:t,command:c,pid:x,status:"running",startedAt:new Date().toISOString()}),r.json?s.json({job_id:a,instance:t,status:"running",pid:x}):(s.info(`Background job started: ${a} (PID ${x})`),s.info(` Poll: gibil job ${a}`));return}s.info(`Running on "${t}" (${o.ip}): ${c}`);let l=await I({instanceName:t,ip:o.ip,command:c,stream:!r.json,timeoutMs:i});r.json?s.json({instance:t,command:c,stdout:l.stdout,stderr:l.stderr,exit_code:l.exitCode}):l.exitCode!==0&&s.error(`Command exited with code ${l.exitCode}`),process.exit(l.exitCode??1)})}tt();J();async function ve(e,t){let n=await yt(t);s.info(`Destroying instance "${t}" (server ${n.serverId})...`);try{await e.destroyServer(n.serverId)}catch(o){s.warn(`Could not delete server ${n.serverId}: ${o instanceof Error?o.message:String(o)}`)}try{await e.deleteSSHKey(n.sshKeyId)}catch(o){s.warn(`Could not delete SSH key ${n.sshKeyId}: ${o instanceof Error?o.message:String(o)}`)}await Q(t),await Lt(t),await bt(t);let r=await P();r&&await X(r,"destroy",t).catch(o=>s.warn(`Usage tracking failed (billing may be inaccurate): ${o instanceof Error?o.message:String(o)}`)),s.info(` ${H} ${k.destroySingle(t)}`)}function $e(e){e.command("destroy [name]").description("Destroy a running ephemeral machine").option("-a, --all","Destroy all gibil instances").option("--json","Output result as JSON").action(async(t,n)=>{if(n.json&&$(!0),n.all){let r=await Y();if(r.length===0){n.json?s.json({destroyed:[],failed:[]}):s.info(k.noInstances);return}let o=await T.create();s.info(`Destroying ${r.length} instance(s)...`);let c=await Promise.allSettled(r.map(a=>ve(o,a.name))),i=[],l=[];for(let a=0;a<c.length;a++)if(c[a].status==="fulfilled")i.push(r[a].name);else{let u=c[a].reason;l.push(`${r[a].name}: ${u instanceof Error?u.message:String(u)}`)}n.json?s.json({destroyed:i,failed:l}):l.length===0?s.info(`
|
|
17
|
+
${k.destroyAll}`):s.info(`
|
|
18
|
+
${i.length} destroyed, ${l.length} failed`)}else{t||(s.error('Specify an instance name or use --all. Run "gibil list" to see instances.'),process.exit(1));let r=await T.create();await ve(r,t),n.json&&s.json({destroyed:[t]})}})}function xe(e){e.command("list").alias("ls").description("List all active gibil instances").option("--json","Output as JSON").action(async t=>{t.json&&$(!0);let n=await Y();if(n.length===0){t.json?s.json({instances:[]}):s.info(k.noInstances);return}let r=n.map(o=>{let c=Math.max(0,Math.floor((new Date(o.expiresAt).getTime()-Date.now())/1e3));return{name:o.name,ip:o.ip,ssh:`ssh -i ${o.keyPath} -o StrictHostKeyChecking=no root@${o.ip}`,status:o.status,ttl_remaining:c,created_at:o.createdAt,fleet_id:o.fleetId}});if(t.json){s.json({instances:r});return}s.info(p(`${"NAME".padEnd(30)} ${"IP".padEnd(18)} ${"STATUS".padEnd(12)} ${"TTL".padEnd(10)} ${"AGE".padEnd(10)}`)),s.info(p("\u2500".repeat(80)));for(let o of r){let c=Se(o.ttl_remaining),i=En(o.created_at),l=o.name.padEnd(30),a=o.status.padEnd(12),u=c.padEnd(10),g=i.padEnd(10),f=o.status==="running"?q(a):R(a),d=o.ttl_remaining<=300?R(u):u;s.info(`${v(l)} ${o.ip.padEnd(18)} ${f} ${d} ${p(g)}`)}s.info(`
|
|
19
|
+
${p(`${r.length} server(s)`)}`)})}function Se(e){if(e<=0)return"expired";let t=Math.floor(e/60),n=e%60;return t>=60?`${Math.floor(t/60)}h ${t%60}m`:`${t}m ${n}s`}function En(e){let t=Date.now()-new Date(e).getTime(),n=Math.floor(t/1e3);return Se(n)}function Ie(e){e.command("extend <name>").description("Extend the TTL of a running instance").requiredOption("--ttl <minutes>","New TTL in minutes from now").option("--json","Output result as JSON").action(async(t,n)=>{n.json&&$(!0);let r=await _(t),o=parseInt(n.ttl,10);(isNaN(o)||o<=0)&&(s.error("TTL must be a positive number of minutes"),process.exit(1)),await I({instanceName:t,ip:r.ip,command:["pkill -f 'sleep.*shutdown' || true",`(sleep ${o*60} && shutdown -h now) &`].join(" && ")});let c=new Date(Date.now()+o*6e4).toISOString();r.ttlMinutes=o,r.expiresAt=c,await W(r),n.json?s.json({name:r.name,ttl_minutes:o,expires_at:c}):s.info(`\u2713 Extended "${t}" TTL to ${o} minutes (expires ${c})`)})}import{readFile as Pn}from"fs/promises";import{randomBytes as An}from"crypto";function ke(e){e.command("exec <name>").description("Upload and run a local script on an instance").requiredOption("-s, --script <path>","Path to local script").option("--json","Output result as JSON").action(async(t,n)=>{n.json&&$(!0);let r=await _(t),o=await Pn(n.script,"utf-8");s.info(`Uploading and running script "${n.script}" on "${t}"...`);let c=Buffer.from(o).toString("base64"),i=`/tmp/gibil-script-${An(4).toString("hex")}.sh`,l=await I({instanceName:t,ip:r.ip,command:`echo '${c}' | base64 -d > ${i} && chmod +x ${i} && ${i}; EXIT=$?; rm -f ${i}; exit $EXIT`,stream:!n.json});n.json?s.json({instance:t,script:n.script,stdout:l.stdout,stderr:l.stderr,exit_code:l.exitCode}):l.exitCode!==0&&s.error(`Script exited with code ${l.exitCode}`),process.exit(l.exitCode??1)})}J();import{createInterface as Tn}from"readline";function _e(e){let t=Tn({input:process.stdin,output:process.stderr});return new Promise(n=>{t.question(e,r=>{t.close(),n(r.trim())})})}function je(e){let t=e.command("auth").description("Manage authentication");t.command("login").description("Log in with your Gibil API key").option("--key <api-key>","API key (or enter interactively)").option("--json","Output result as JSON").action(async n=>{n.json&&$(!0);let r=n.key??process.env.GIBIL_API_KEY;r||(r=await _e("Enter your API key: ")),r||(s.error("No API key provided."),process.exit(1)),r.startsWith("pk_")||(s.error('Invalid key format. API keys start with "pk_".'),process.exit(1)),s.info("Verifying API key...");try{let o=await F(r);await ot(r),n.json?s.json({authenticated:!0,email:o.user.email,plan:o.user.plan}):(s.info(k.authSuccess),s.detail("Email",o.user.email),s.detail("Plan",o.user.plan),s.detail("Limits",`${o.limits.max_concurrent} concurrent servers, ${o.limits.remaining_hours}h remaining`))}catch(o){s.error(o instanceof Error?o.message:String(o)),process.exit(1)}}),t.command("setup").description("Configure Hetzner API token (stored in ~/.gibil/config.json)").option("--token <token>","Hetzner API token (or enter interactively)").action(async n=>{let r=n.token;r||(r=await _e("Enter your Hetzner API token: ")),r||(s.error("No token provided."),process.exit(1));try{let c=await(await fetch("https://api.hetzner.cloud/v1/servers",{headers:{Authorization:`Bearer ${r}`}})).json();c.error&&(s.error(`Invalid token: ${c.error.message}`),process.exit(1))}catch{s.error("Could not verify token with Hetzner API."),process.exit(1)}await rt(r),s.success("Hetzner token saved to ~/.gibil/config.json")}),t.command("logout").description("Clear stored API key").action(async()=>{await Tt(),s.info(k.authLogout)}),t.command("status").description("Show current authentication status").option("--json","Output result as JSON").action(async n=>{n.json&&$(!0);let r=await P();if(!r){n.json?s.json({authenticated:!1}):s.info(`Not logged in. Run ${v("gibil auth login")} to authenticate.`);return}try{let o=await F(r);n.json?s.json({authenticated:!0,email:o.user.email,plan:o.user.plan,limits:o.limits}):(s.success(`Authenticated as ${o.user.email}`),s.detail("Plan",o.user.plan),s.detail("Concurrent servers",String(o.limits.max_concurrent)),s.detail("Hours remaining",String(o.limits.remaining_hours)))}catch{n.json?s.json({authenticated:!1,error:"Key verification failed"}):s.error(`Stored API key is invalid. Run ${v("gibil auth login")} to re-authenticate.`)}})}J();function Ce(e){e.command("usage").description("Show current month's usage and plan limits").option("--json","Output as JSON").action(async t=>{t.json&&$(!0);let n=await P();n||(s.error('Not logged in. Run "gibil auth login" first.'),process.exit(1));try{let r=await Ht(n);if(t.json)s.json(r);else{let o=Math.round(r.vm_hours_used/r.vm_hours_limit*100);s.info(`Plan: ${r.plan}`),s.info(`VM hours: ${r.vm_hours_used.toFixed(1)} / ${r.vm_hours_limit}h (${o}%)`),s.info(`Active instances: ${r.active_instances} / ${r.max_concurrent}`),o>80&&s.warn("Running low on hours. Upgrade at https://gibil.dev/pricing")}}catch(r){s.error(r instanceof Error?r.message:String(r)),process.exit(1)}})}import{McpServer as On}from"@modelcontextprotocol/sdk/server/mcp.js";import{StdioServerTransport as Nn}from"@modelcontextprotocol/sdk/server/stdio.js";import{z as y}from"zod";M();import{execSync as St}from"child_process";import{readFileSync as Hn}from"fs";tt();tt();async function Gt(e){let t=await U(e);if(t.status!=="running")return{status:t.status,exitCode:t.exitCode};let n=await _(t.instance),r="/root/.gibil-jobs",o=`${r}/${e}.exit`,c=`${r}/${e}.log`,l=(await I({instanceName:t.instance,ip:n.ip,command:`test -f ${o} && cat ${o} || echo RUNNING`,timeoutMs:1e4})).stdout.trim();if(l==="RUNNING")return{status:"running"};let a=parseInt(l,10),u=await I({instanceName:t.instance,ip:n.ip,command:`cat ${c} 2>/dev/null || echo ''`,timeoutMs:1e4}),g=a===0?"done":"failed",f=new Date,d=Math.round((f.getTime()-new Date(t.startedAt).getTime())/1e3);return t.status=g,t.exitCode=a,t.completedAt=f.toISOString(),await z(t),{status:g,exitCode:a,stdout:u.stdout,durationS:d}}function Ee(e){let t=e.command("job").description("Manage background jobs");t.command("status <id>").description("Check status of a background job").option("--json","Output result as JSON").action(async(n,r)=>{r.json&&$(!0);let o=await U(n),c=await Gt(n);r.json?s.json({job_id:n,instance:o.instance,command:o.command,status:c.status,exit_code:c.exitCode,started_at:o.startedAt,duration_s:c.durationS,...c.stdout!==void 0?{stdout:c.stdout}:{}}):c.status==="running"?(s.info(`Job ${n} is still running on "${o.instance}"`),s.info(` Command: ${o.command}`),s.info(` Started: ${o.startedAt}`)):(s.info(`Job ${n}: ${c.status} (exit code ${c.exitCode}, ${c.durationS}s)`),c.stdout&&process.stdout.write(c.stdout))}),t.command("list").description("List all background jobs").option("--json","Output result as JSON").action(async n=>{n.json&&$(!0);let r=await at();if(r.length===0){n.json?s.json([]):s.info("No background jobs.");return}if(n.json)s.json(r.map(o=>({job_id:o.id,instance:o.instance,command:o.command,status:o.status,started_at:o.startedAt,exit_code:o.exitCode})));else for(let o of r){let c=o.status==="running"?"\u27F3 running":o.status==="done"?"\u2713 done":`\u2717 ${o.status}`;s.info(` ${o.id} ${c} ${o.instance} ${o.command}`)}}),t.command("cancel <id>").description("Cancel a running background job").option("--json","Output result as JSON").action(async(n,r)=>{r.json&&$(!0);let o=await U(n);if(o.status!=="running"){r.json?s.json({job_id:n,status:o.status,message:"Job is not running"}):s.info(`Job ${n} is not running (status: ${o.status})`);return}let c=await _(o.instance);await I({instanceName:o.instance,ip:c.ip,command:`kill -- -${o.pid} 2>/dev/null || kill ${o.pid} 2>/dev/null || true`,timeoutMs:1e4}),o.status="cancelled",o.completedAt=new Date().toISOString(),await z(o),r.json?s.json({job_id:n,status:"cancelled"}):s.info(`Job ${n} cancelled.`)}),t.command("logs <id>").description("Fetch output of a background job").option("--json","Output result as JSON").option("-f, --follow","Follow log output (tail -f)").action(async(n,r)=>{r.json&&$(!0);let o=await U(n),c=await _(o.instance),i=`/root/.gibil-jobs/${n}.log`,l=r.follow?`tail -f ${i}`:`cat ${i} 2>/dev/null || echo '(no output yet)'`,a=r.follow?3e5:1e4,u=await I({instanceName:o.instance,ip:c.ip,command:l,stream:!r.json,timeoutMs:a});r.json&&s.json({job_id:n,stdout:u.stdout})})}function Mn(){try{let e=St("git config user.name",{encoding:"utf-8"}).trim(),t=St("git config user.email",{encoding:"utf-8"}).trim();if(!e||!t)return;let n;try{if(St("git config gpg.format",{encoding:"utf-8"}).trim()==="ssh"){let o=St("git config user.signingkey",{encoding:"utf-8"}).trim();if(o)try{n=Hn(o,"utf-8").trim()}catch{(o.startsWith("ssh-")||o.startsWith("key::"))&&(n=o.replace(/^key::/,""))}}}catch{}return{name:e,email:t,signingKey:n}}catch{return}}async function ct(e,t){if(e)return e;if(t)return _(t);let r=(await Y()).filter(o=>new Date<new Date(o.expiresAt));if(r.length===0)throw new Error("No active servers. Use create_server first.");if(r.length===1)return r[0];throw new Error(`Multiple servers running: ${r.map(o=>o.name).join(", ")}. Pass the "server" parameter to specify which one.`)}function B(e,t,n=3e4){return I({instanceName:e.name,ip:e.ip,command:t,stream:!1,timeoutMs:n})}function O(e){return`'${e.replace(/'/g,"'\\''")}'`}async function Pe(e){let t=null;if(e&&(t=await _(e),t.gitIdentity)){let{name:i,email:l,signingKey:a}=t.gitIdentity,u=[`git config --global user.name ${O(i)}`,`git config --global user.email ${O(l)}`];a&&u.push("git config --global gpg.format ssh",`git config --global user.signingkey ${O("key::"+a)}`,"git config --global commit.gpgsign true"),B(t,u.join(" && ")).catch(()=>{})}let n=e?`gibil-${e}`:"gibil",r=new On({name:n,version:"0.4.0"});t||(r.tool("create_server","Forge a new ephemeral server. Clones repo, installs deps, and waits until fully provisioned before returning. Returns the server name, IP, and provisioning status.",{name:y.string().optional().describe("Server name (auto-generated if omitted)"),repo:y.string().optional().describe("Git repo URL to clone on boot"),ttl:y.number().optional().describe("Auto-destroy after N minutes (default: 60)"),server_type:y.string().optional().describe("Hetzner server type (default: auto-detected)"),location:y.string().optional().describe("Hetzner datacenter (default: auto-detected)"),env:y.record(y.string(),y.string()).optional().describe("Environment variables to set on the server")},async({name:i,repo:l,ttl:a,server_type:u,location:g,env:f})=>{try{let d=i??wt(),m=a??60,h=await T.create(),b=await mt(d),C=await h.createSSHKey(`gibil-${d}`,b.publicKey),w=Mn(),x=f?{env:f}:void 0,kt=ft({repo:l,config:x,ttlMinutes:m,githubToken:process.env.GITHUB_TOKEN,gitIdentity:w}),lt=await h.createServer(d,C.id,kt,u,g),N=(await h.waitForReady(lt.id)).public_net.ipv4.ip,zt=new Date,He={name:d,serverId:lt.id,ip:N,sshKeyId:C.id,keyPath:S.privateKey(d),status:"running",createdAt:zt.toISOString(),ttlMinutes:m,expiresAt:new Date(zt.getTime()+m*6e4).toISOString(),repo:l,gitIdentity:w};await W(He),await gt(d,N);let _t="ready";if(l||x){let Me=Date.now(),Ut=!1;for(;Date.now()-Me<36e4;){try{if((await I({instanceName:d,ip:N,command:"test -f /root/.gibil-ready && echo ready || echo waiting",timeoutMs:1e4})).stdout.trim()==="ready"){Ut=!0;break}}catch{}await new Promise(jt=>setTimeout(jt,5e3))}if(!Ut){_t="timeout";try{_t=`timeout \u2014 cloud-init log:
|
|
20
|
+
${(await I({instanceName:d,ip:N,command:"tail -20 /var/log/cloud-init-output.log 2>/dev/null || echo 'no log'",timeoutMs:1e4})).stdout}`}catch{}}}return{content:[{type:"text",text:JSON.stringify({name:d,ip:N,ttl_minutes:m,status:"running",provisioning:_t},null,2)}]}}catch(d){return{content:[{type:"text",text:`Failed to create server: ${d instanceof Error?d.message:String(d)}`}],isError:!0}}}),r.tool("destroy_server","Burn a server. Deletes the server, SSH keys, and local metadata.",{name:y.string().describe("Name of the server to destroy")},async({name:i})=>{try{let l=await yt(i),a=await T.create();await a.destroyServer(l.serverId).catch(()=>{}),await a.deleteSSHKey(l.sshKeyId).catch(()=>{}),await Q(i).catch(()=>{});let{deleteJobsByInstance:u}=await Promise.resolve().then(()=>(tt(),be));return await u(i).catch(()=>{}),await bt(i),{content:[{type:"text",text:`Server "${i}" destroyed.`}]}}catch(l){return{content:[{type:"text",text:`Failed to destroy: ${l instanceof Error?l.message:String(l)}`}],isError:!0}}}),r.tool("list_servers","List all active gibil servers with their names, IPs, and remaining TTL.",{},async()=>{let l=(await Y()).map(a=>({name:a.name,ip:a.ip,status:a.status,ttl_remaining_seconds:Math.max(0,Math.floor((new Date(a.expiresAt).getTime()-Date.now())/1e3)),repo:a.repo}));return{content:[{type:"text",text:JSON.stringify(l,null,2)}]}}),r.tool("extend_server","Extend a server's TTL. Resets the auto-destroy timer.",{name:y.string().describe("Server name"),ttl:y.number().describe("New TTL in minutes from now")},async({name:i,ttl:l})=>{try{let a=await _(i),u=await B(a,`pkill -f 'sleep.*shutdown' || true && (sleep ${l*60} && shutdown -h now) &`);return u.exitCode!==0?{content:[{type:"text",text:`Failed to extend TTL: ${u.stderr}`}],isError:!0}:(a.ttlMinutes=l,a.expiresAt=new Date(Date.now()+l*6e4).toISOString(),await W(a),{content:[{type:"text",text:`Server "${i}" TTL extended to ${l} minutes.`}]})}catch(a){return{content:[{type:"text",text:`Failed to extend: ${a instanceof Error?a.message:String(a)}`}],isError:!0}}}));let o=y.string().optional().describe("Server name (auto-selects if only one is running)");r.tool("vm_bash","Run a shell command on a remote server. Use for: builds, tests, git, package managers, any shell operation. Set background=true for long-running commands \u2014 returns a job ID you can poll with vm_job_status.",{command:y.string().describe("Shell command to execute"),working_dir:y.string().optional().describe("Working directory (default: /root/project)"),timeout_ms:y.number().optional().describe("Timeout in ms (default: 120000). Increase for long builds or test suites."),background:y.boolean().optional().describe("Run in background, return job ID for polling"),server:o},async i=>{let l=await ct(t,i.server),u=`cd ${i.working_dir??"/root/project"} 2>/dev/null || cd /root && ${i.command}`;if(i.background){let d=vt(),m="/root/.gibil-jobs",h=`${m}/${d}.log`,b=`${m}/${d}.exit`,C=`${m}/${d}.pid`,w=`${m}/${d}.sh`,x=["#!/bin/bash",`nohup bash -c '${u.replace(/'/g,"'\\''")}' > ${h} 2>&1 &`,"BGPID=$!",`echo $BGPID > ${C}`,`(wait $BGPID 2>/dev/null; echo $? > ${b}) &`,"echo $BGPID"].join(`
|
|
21
|
+
`),kt=Buffer.from(x).toString("base64"),lt=`mkdir -p ${m} && echo '${kt}' | base64 -d > ${w} && chmod +x ${w} && bash ${w}`,Jt=await B(l,lt,1e4),N=parseInt(Jt.stdout.trim(),10);return isNaN(N)?{content:[{type:"text",text:"Failed to start background job \u2014 could not capture PID"}],isError:!0}:(await z({id:d,instance:l.name,command:i.command,pid:N,status:"running",startedAt:new Date().toISOString()}),{content:[{type:"text",text:JSON.stringify({job_id:d,instance:l.name,status:"running",pid:N},null,2)}]})}let g=await B(l,u,i.timeout_ms??12e4);return{content:[{type:"text",text:[g.stdout,g.stderr].filter(Boolean).join(`
|
|
22
|
+
`)||"(no output)"}],isError:g.exitCode!==0}}),r.tool("vm_job_status","Check the status of a background job started with vm_bash(background=true). Returns status, exit code, and output when done.",{job_id:y.string().describe("Job ID returned by vm_bash with background=true")},async i=>{try{let l=await U(i.job_id),a=await Gt(i.job_id);return{content:[{type:"text",text:JSON.stringify({job_id:i.job_id,instance:l.instance,command:l.command,status:a.status,exit_code:a.exitCode,started_at:l.startedAt,duration_s:a.durationS,...a.stdout!==void 0?{stdout:a.stdout}:{}},null,2)}],isError:a.status==="failed"}}catch(l){return{content:[{type:"text",text:`Error: ${l instanceof Error?l.message:String(l)}`}],isError:!0}}}),r.tool("vm_job_list","List all background jobs across all servers.",{},async()=>{let l=(await at()).map(a=>({job_id:a.id,instance:a.instance,command:a.command,status:a.status,started_at:a.startedAt,exit_code:a.exitCode}));return{content:[{type:"text",text:JSON.stringify(l,null,2)}]}}),r.tool("vm_read","Read a file from a remote server. Returns the file contents with line numbers.",{path:y.string().describe("Absolute path on the server (e.g. /root/project/src/app.ts)"),offset:y.number().optional().describe("Start at line N (1-based)"),limit:y.number().optional().describe("Max lines to return"),server:o},async i=>{let l=await ct(t,i.server),a=O(i.path),u=`cat -n ${a}`;i.offset&&i.limit?u=`sed -n '${i.offset},${i.offset+i.limit-1}p' ${a} | cat -n`:i.offset?u=`tail -n +${i.offset} ${a} | cat -n`:i.limit&&(u=`head -n ${i.limit} ${a} | cat -n`);let g=await B(l,u);return g.exitCode!==0?{content:[{type:"text",text:`Error: ${g.stderr}`}],isError:!0}:{content:[{type:"text",text:g.stdout}]}}),r.tool("vm_write","Write content to a file on a remote server. Creates parent directories if needed. Overwrites existing files.",{path:y.string().describe("Absolute path on the server"),content:y.string().describe("File content to write"),server:o},async i=>{let l=await ct(t,i.server),a=Buffer.from(i.content).toString("base64"),u=O(i.path),g=`mkdir -p "$(dirname ${u})" && echo '${a}' | base64 -d > ${u}`,f=await B(l,g);return f.exitCode!==0?{content:[{type:"text",text:`Error: ${f.stderr}`}],isError:!0}:{content:[{type:"text",text:`Wrote ${i.path}`}]}}),r.tool("vm_ls","List files and directories on a remote server.",{path:y.string().optional().describe("Directory path (default: /root/project)"),glob:y.string().optional().describe("Glob pattern to filter (e.g. '**/*.ts')"),server:o},async i=>{let l=await ct(t,i.server),a=i.path??"/root/project",u;i.glob?u=`cd ${O(a)} && find . -path './${i.glob}' -type f 2>/dev/null | sort | head -200`:u=`ls -la ${O(a)}`;let g=await B(l,u);return g.exitCode!==0?{content:[{type:"text",text:`Error: ${g.stderr}`}],isError:!0}:{content:[{type:"text",text:g.stdout}]}}),r.tool("vm_grep","Search for a pattern in files on a remote server. Uses ripgrep if available, falls back to grep.",{pattern:y.string().describe("Regex pattern to search for"),path:y.string().optional().describe("Directory or file to search (default: /root/project)"),include:y.string().optional().describe("File glob to include (e.g. '*.ts')"),server:o},async i=>{let l=await ct(t,i.server),a=i.path??"/root/project",u=O(i.pattern),g=O(a),f;if(i.include){let h=O(i.include);f=`cd ${g} && (rg -n --glob ${h} ${u} 2>/dev/null || grep -rn --include=${h} ${u} .) | head -100`}else f=`cd ${g} && (rg -n ${u} 2>/dev/null || grep -rn ${u} .) | head -100`;return{content:[{type:"text",text:(await B(l,f)).stdout||"(no matches)"}]}});let c=new Nn;await r.connect(c)}function Ae(e){e.command("mcp [name]").description("Start an MCP server (used by Claude Code, Cursor, and other agents)").action(async t=>{try{await Pe(t)}catch(n){s.error(n instanceof Error?n.message:String(n)),process.exit(1)}})}J();import{createInterface as Rn}from"readline";import{execSync as Kn}from"child_process";import{existsSync as Dn,readFileSync as Ln,writeFileSync as Gn,mkdirSync as Fn}from"fs";import{join as Ft}from"path";import{homedir as Jn}from"os";M();function It(e){let t=Rn({input:process.stdin,output:process.stderr});return new Promise(n=>{t.question(e,r=>{t.close(),n(r.trim())})})}async function zn(){let e=!!await Ot(),t=!!await P();return{hetzner:e,apiKey:t}}function Te(e){e.command("init").description("Set up gibil \u2014 configure your forge in 60 seconds").option("--force","Reconfigure even if already set up").action(async t=>{console.error(Yt);let n=await zn();if(n.hetzner&&!t.force){s.info(`${H} Already configured.`),n.apiKey?(s.detail("Hetzner",q("connected")),s.detail("Gibil API",q("connected"))):(s.detail("Hetzner",q("connected")),s.detail("Gibil API",p("not configured (optional)"))),s.info(""),s.info(` Run ${v("gibil init --force")} to reconfigure.`),s.info(` Run ${v("gibil create")} to forge a server.`);return}s.info(""),s.info(v("Step 1: Hetzner API Token")),s.info(p(" Your servers run on Hetzner Cloud. You need an API token.")),s.info(p(" Get one at: https://console.hetzner.cloud \u2192 API Tokens")),s.info("");let r=await It(" Hetzner API token: ");r||(s.error("No token provided. Run gibil init again when ready."),process.exit(1));let o=s.spin("Verifying Hetzner token...");try{let m=await(await fetch("https://api.hetzner.cloud/v1/servers",{headers:{Authorization:`Bearer ${r}`}})).json();m.error&&(o.fail(`Invalid token: ${m.error.message}`),process.exit(1)),o.succeed("Hetzner token verified")}catch{o.fail("Could not reach Hetzner API. Check your network."),process.exit(1)}await rt(r);let c=s.spin("Detecting available server types..."),i="cax11",l="fsn1",a=[{type:"cax11",location:"fsn1"},{type:"cpx11",location:"fsn1"}];for(let d of a)try{let h=await(await fetch("https://api.hetzner.cloud/v1/servers",{method:"POST",headers:{Authorization:`Bearer ${r}`,"Content-Type":"application/json"},body:JSON.stringify({name:"gibil-probe",server_type:d.type,image:"ubuntu-24.04",location:d.location,start_after_create:!1})})).json();if(h.server){await fetch(`https://api.hetzner.cloud/v1/servers/${h.server.id}`,{method:"DELETE",headers:{Authorization:`Bearer ${r}`}}),i=d.type,l=d.location;break}}catch{}await Nt(i,l),c.succeed(`Default server type: ${i} (${l})`),s.info(""),s.info(v("Step 2: Gibil API Key")+p(" (optional)")),s.info(p(" For usage tracking and plan limits.")),s.info(p(" Skip this if you just want to use your own Hetzner token.")),s.info("");let u=await It(" Gibil API key (or press Enter to skip): ");if(u)if(!u.startsWith("pk_"))s.warn('API keys start with "pk_". Skipping.');else{let d=s.spin("Verifying API key...");try{let m=await F(u);await ot(u),d.succeed(`Logged in as ${m.user.email} (${m.user.plan})`)}catch(m){d.fail(`Could not verify key: ${m instanceof Error?m.message:String(m)}`),s.info(p(" Continuing without Gibil API. You can add it later with: gibil auth login"))}}else s.info(p(" Skipped. You can add it later with: gibil auth login"));if(s.info(""),s.info(k.initComplete),s.info(""),s.info(p(" Quick start:")),s.info(` ${v("gibil create")} ${p("Forge a server")}`),s.info(` ${v("gibil create --repo github.com/you/project")} ${p("Clone a repo on boot")}`),s.info(` ${v("gibil ssh <name>")} ${p("Connect to it")}`),s.info(` ${v("gibil destroy <name>")} ${p("Burn it down")}`),s.info(""),(await It(" Install the gibil agent skill? (Y/n): ")).toLowerCase()!=="n"){let d=s.spin("Installing gibil skill...");try{Kn("npx -y skills add https://github.com/AlexikM/gibil-skills --skill gibil -y -g",{stdio:"pipe",timeout:3e4}),d.succeed("Gibil skill installed for your AI agents")}catch{d.fail("Could not install skill automatically"),s.info(p(" Install manually: npx skills add https://github.com/AlexikM/gibil-skills --skill gibil"))}}else s.info(p(" Skipped. Install later: npx skills add https://github.com/AlexikM/gibil-skills --skill gibil"));if(s.info(""),(await It(" Configure gibil MCP server for Claude Code? (Y/n): ")).toLowerCase()!=="n"){let d=s.spin("Configuring MCP server...");try{let m=Ft(Jn(),".claude"),h=Ft(m,".mcp.json");Fn(m,{recursive:!0});let b={};try{b=JSON.parse(Ln(h,"utf-8"))}catch{}b.mcpServers||(b.mcpServers={}),b.mcpServers.gibil={command:"gibil",args:["mcp"]},Gn(h,JSON.stringify(b,null,2)+`
|
|
23
|
+
`),d.succeed("MCP server configured \u2014 restart Claude Code to activate")}catch(m){d.fail(`Could not configure MCP: ${m instanceof Error?m.message:String(m)}`),s.info(p(' Add manually to ~/.claude/.mcp.json: { "mcpServers": { "gibil": { "command": "gibil", "args": ["mcp"] } } }'))}}else s.info(p(' Skipped. Add manually to ~/.claude/.mcp.json: { "mcpServers": { "gibil": { "command": "gibil", "args": ["mcp"] } } }'))})}async function Oe(){if(process.env.HETZNER_API_TOKEN)return!1;let e=Ft(S.root,"config.json");return!Dn(e)}try{await import("dotenv/config")}catch{}var Vn=Wn(qn(import.meta.url)),Ne={version:"0.0.0"};for(let e of["../package.json","../../package.json"])try{Ne=JSON.parse(Bn(Yn(Vn,e),"utf-8"));break}catch{}var j=new Un;j.name("gibil").description("Ephemeral dev compute for humans and AI agents").version(`${Ne.version} ${ut}`,"-v, --version").addHelpText("before",`
|
|
24
|
+
${Vt}
|
|
21
25
|
`).addHelpText("after",`
|
|
22
26
|
${p("Docs:")} https://gibil.dev/docs
|
|
23
|
-
`);
|
|
27
|
+
`);Te(j);me(j);ge(j);we(j);$e(j);xe(j);Ie(j);ke(j);je(j);Ce(j);Ee(j);Ae(j);async function Zn(){let e=process.argv.slice(2);!(e.length===0||e.includes("init")||e.includes("auth")||e.includes("--help")||e.includes("-h")||e.includes("--version")||e.includes("-v")||e.includes("-V")||e.includes("mcp")||e.includes("ssh")||e.includes("run")||e.includes("exec")||e.includes("list")||e.includes("ls"))&&await Oe()&&(s.info(""),s.info(k.setupNeeded),s.info(""),process.exit(1));try{await j.parseAsync(process.argv)}catch(n){n instanceof Error&&s.error(n.message),process.exit(1)}}Zn();
|