@medplum/cli 2.0.24 → 2.0.25
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/dist/cjs/index.cjs +6 -1495
- package/dist/cjs/index.cjs.map +7 -1
- package/dist/esm/index.mjs +9 -0
- package/dist/esm/index.mjs.map +7 -0
- package/dist/esm/package.json +1 -0
- package/dist/types/auth.d.ts +2 -0
- package/dist/types/aws/describe.d.ts +5 -0
- package/dist/types/aws/index.d.ts +2 -0
- package/dist/types/aws/init.d.ts +1 -0
- package/dist/types/aws/list.d.ts +4 -0
- package/dist/types/aws/update-app.d.ts +5 -0
- package/dist/types/aws/update-server.d.ts +5 -0
- package/dist/types/aws/utils.d.ts +48 -0
- package/dist/types/bots.d.ts +7 -0
- package/dist/types/bulk.d.ts +2 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/project.d.ts +2 -0
- package/dist/types/rest.d.ts +6 -0
- package/dist/types/storage.d.ts +11 -0
- package/dist/types/util/client.d.ts +3 -0
- package/dist/types/util/command.d.ts +2 -0
- package/dist/types/utils.d.ts +36 -0
- package/package.json +2 -2
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,1498 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
'use strict';
|
|
2
|
+
"use strict";var vt=Object.create;var I=Object.defineProperty;var bt=Object.getOwnPropertyDescriptor;var Et=Object.getOwnPropertyNames;var Mt=Object.getPrototypeOf,kt=Object.prototype.hasOwnProperty;var Pt=(e,t)=>{for(var o in t)I(e,o,{get:t[o],enumerable:!0})},ye=(e,t,o,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of Et(t))!kt.call(e,a)&&a!==o&&I(e,a,{get:()=>t[a],enumerable:!(n=bt(t,a))||n.enumerable});return e};var P=(e,t,o)=>(o=e!=null?vt(Mt(e)):{},ye(t||!e||!e.__esModule?I(o,"default",{value:e,enumerable:!0}):o,e)),xt=e=>ye(I({},"__esModule",{value:!0}),e);var ao={};Pt(ao,{main:()=>wt,run:()=>Ct});module.exports=xt(ao);var q=require("@medplum/core"),ht=require("commander"),St=P(require("dotenv"));var T=require("@medplum/core"),ve=require("os"),be=require("child_process"),Ee=require("http");var he=require("commander");function m(e){return new he.Command(e).option("--client-id <clientId>","FHIR server client id").option("--client-secret <clientSecret>","FHIR server client secret").option("--base-url <baseUrl>","FHIR server base url").option("--token-url <tokenUrl>","FHIR server token url").option("--authorize-url <authorizeUrl>","FHIR server authorize url").option("--fhir-url-path <fhirUrlPath>","FHIR server url path")}var Ce=require("@medplum/core");var Se=require("@medplum/core"),h=require("fs"),we=require("os"),G=require("path"),R=class extends Se.ClientStorage{constructor(){super();this.dirName=(0,G.resolve)((0,we.homedir)(),".medplum"),this.fileName=(0,G.resolve)(this.dirName,"credentials")}clear(){this.writeFile({})}getString(o){return this.readFile()?.[o]}setString(o,n){let a=this.readFile()||{};n?a[o]=n:delete a[o],this.writeFile(a)}readFile(){if((0,h.existsSync)(this.fileName))return JSON.parse((0,h.readFileSync)(this.fileName,"utf8"))}writeFile(o){(0,h.existsSync)(this.dirName)||(0,h.mkdirSync)(this.dirName),(0,h.writeFileSync)(this.fileName,JSON.stringify(o,null,2),"utf8")}};async function l(e){let t=e.baseUrl??process.env.MEDPLUM_BASE_URL??"https://api.medplum.com/",o=e.fhirUrlPath??process.env.MEDPLUM_FHIR_URL_PATH??"",n=e.accessToken??process.env.MEDPLUM_CLIENT_ACCESS_TOKEN??"",a=e.tokenUrl??process.env.MEDPLUM_TOKEN_URL??"",i=e.authorizeUrl??process.env.MEDPLUM_AUTHORIZE_URL??"",s=e.fetch??fetch,c=new Ce.MedplumClient({fetch:s,baseUrl:t,tokenUrl:a,fhirUrlPath:o,authorizeUrl:i,storage:new R,onUnauthenticated:At});n&&c.setAccessToken(n);let u=e.clientId||process.env.MEDPLUM_CLIENT_ID,y=e.clientSecret||process.env.MEDPLUM_CLIENT_SECRET;return u&&y&&(c.setBasicAuth(u,y),await c.startClientLogin(u,y)),c}function At(){console.log("Unauthenticated: run `npx medplum login` to sign in")}var Me="medplum-cli",ke="http://localhost:9615",H=m("login"),V=m("whoami");H.action(async e=>{let t=await l(e);await Dt(t)});V.action(async e=>{let t=await l(e);Rt(t)});async function Dt(e){await Nt(e);let t=new URL(e.getAuthorizeUrl());t.searchParams.set("client_id",Me),t.searchParams.set("redirect_uri",ke),t.searchParams.set("scope","openid"),t.searchParams.set("response_type","code"),t.searchParams.set("prompt","login"),await It(t.toString())}async function Nt(e){let t=(0,Ee.createServer)(async(o,n)=>{let a=new URL(o.url,"http://localhost:9615"),i=a.searchParams.get("code");if(a.pathname==="/"&&i)try{let s=await e.processCode(i,{clientId:Me,redirectUri:ke});n.writeHead(200,{"Content-Type":"text/plain"}),n.end(`Signed in as ${(0,T.getDisplayString)(s)}. You may close this window.`)}catch(s){n.writeHead(400,{"Content-Type":"text/plain"}),n.end(`Error: ${(0,T.normalizeErrorString)(s)}`)}finally{t.close()}else n.writeHead(404,{"Content-Type":"text/plain"}),n.end("Not found")}).listen(9615)}async function It(e){let t=(0,ve.platform)(),o;switch(t){case"openbsd":case"linux":o=`xdg-open '${e}'`;break;case"darwin":o=`open '${e}'`;break;case"win32":o=`cmd /c start "" "${e}"`;break;default:throw new Error("Unsupported platform: "+t)}(0,be.exec)(o)}function Rt(e){let t=e.getActiveLogin();t?(console.log(`Server: ${e.getBaseUrl()}`),console.log(`Profile: ${t.profile.display} (${t.profile.reference})`),console.log(`Project: ${t.project.display} (${t.project.reference})`)):console.log("Not logged in")}var Qe=require("commander");var w=require("@aws-sdk/client-cloudformation"),Pe=require("@aws-sdk/client-cloudfront"),xe=require("@aws-sdk/client-ecs"),Ae=require("@aws-sdk/client-s3"),Y=new w.CloudFormationClient({}),De=new Pe.CloudFrontClient({}),Ne=new xe.ECSClient({}),Ie=new Ae.S3Client({}),Tt="medplum:environment";async function J(){return(await Y.send(new w.ListStacksCommand({}))).StackSummaries?.filter(t=>t.StackName&&t.StackStatus!=="DELETE_COMPLETE")||[]}async function E(e){let t=await J();for(let o of t){let n=o.StackName,a=await X(n);if(a?.tag===e)return a}}async function X(e){let t=new w.DescribeStacksCommand({StackName:e}),n=(await Y.send(t))?.Stacks?.[0],a=n?.Tags?.find(c=>c.Key===Tt);if(!a)return;let i=await Y.send(new w.DescribeStackResourcesCommand({StackName:e}));if(!i.StackResources)return;let s={stack:n,tag:a.Value};for(let c of i.StackResources)c.ResourceType==="AWS::ECS::Cluster"?s.ecsCluster=c:c.ResourceType==="AWS::ECS::Service"?s.ecsService=c:c.ResourceType==="AWS::S3::Bucket"&&c.LogicalResourceId?.startsWith("FrontEndAppBucket")?s.appBucket=c:c.ResourceType==="AWS::S3::Bucket"&&c.LogicalResourceId?.startsWith("StorageStorageBucket")?s.storageBucket=c:c.ResourceType==="AWS::CloudFront::Distribution"&&c.LogicalResourceId?.startsWith("FrontEndAppDistribution")&&(s.appDistribution=c);return s}function B(e){console.log(`Medplum Tag: ${e.tag}`),console.log(`Stack Name: ${e.stack.StackName}`),console.log(`Stack ID: ${e.stack.StackId}`),console.log(`Status: ${e.stack.StackStatus}`),console.log(`ECS Cluster: ${e.ecsCluster?.PhysicalResourceId}`),console.log(`ECS Service: ${Z(e.ecsService)}`),console.log(`App Bucket: ${e.appBucket?.PhysicalResourceId}`),console.log(`Storage Bucket: ${e.storageBucket?.PhysicalResourceId}`)}function Z(e){return e?.PhysicalResourceId?.split("/")?.pop()||""}async function Re(e){let t=await E(e);if(!t){console.log("Stack not found");return}B(t)}var C=require("@aws-sdk/client-acm"),M=require("@aws-sdk/client-ssm"),j=require("@aws-sdk/client-sts"),$=require("crypto"),L=require("fs"),Be=require("path"),Ue=P(require("readline")),Bt=e=>`${e}DomainName`,Fe=e=>`${e}SslCertArn`,F;async function je(){let e={apiPort:8103,region:"us-east-1"};F=Ue.default.createInterface({input:process.stdin,output:process.stdout}),d("MEDPLUM"),r("This tool prepares the necessary prerequisites for deploying Medplum in your AWS account."),r(""),r("Most Medplum infrastructure is deployed using the AWS CDK."),r("However, some AWS resources must be created manually, such as email addresses and SSL certificates."),r("This tool will help you create those resources."),r(""),r("Upon completion, this tool will:"),r(" 1. Generate a Medplum CDK config file (i.e., medplum.demo.config.json)"),r(" 2. Optionally generate an AWS CloudFront signing key"),r(" 3. Optionally request SSL certificates from AWS Certificate Manager"),r(" 4. Optionally write server config settings to AWS Parameter Store"),r(""),r("The Medplum infra config file is an input to the Medplum CDK."),r("The Medplum CDK will create and manage the necessary AWS resources."),r(""),r("We will ask a series of questions to generate your infra config file."),r("Some questions have predefined options in [square brackets]."),r("Some questions have default values in (parentheses), which you can accept by pressing Enter."),r("Press Ctrl+C at any time to exit.");let t=await Ut(e.region);t||(r("It appears that you do not have AWS credentials configured."),r("AWS credentials are not strictly required, but will enable some additional features."),r("If you intend to use AWS credentials, please configure them now."),await U("Do you want to continue without AWS credentials?")),d("ENVIRONMENT NAME"),r('Medplum deployments have a short environment name such as "prod", "staging", "alice", or "demo".'),r("The environment name is used in multiple places:"),r(" 1. As part of config file names (i.e., medplum.demo.config.json)"),r(" 2. As the base of CloudFormation stack names (i.e., MedplumDemo)"),r(" 3. AWS Parameter Store keys (i.e., /medplum/demo/...)"),e.name=await f("What is your environment name?","demo"),r('Using environment name "'+e.name+'"...'),d("CONFIG FILE"),r("Medplum Infrastructure will create a config file in the current directory.");let o=await f("What is the config file name?",`medplum.${e.name}.config.json`);(0,L.existsSync)(o)&&(r("Config file already exists."),await U("Do you want to overwrite the config file?")),r('Using config file "'+o+'"...'),p(o,e),d("AWS REGION"),r("Most Medplum resources will be created in a single AWS region."),e.region=await f("Enter your AWS region:","us-east-1"),p(o,e),d("AWS ACCOUNT NUMBER"),r("Medplum Infrastructure will use your AWS account number to create AWS resources."),t&&r("Using the AWS CLI, your current account ID is: "+t),e.accountNumber=await f("What is your AWS account number?",t),p(o,e),d("STACK NAME"),r("Medplum will create a CloudFormation stack to manage AWS resources."),r("AWS CloudFormation stack names ");let n="Medplum"+e.name.charAt(0).toUpperCase()+e.name.slice(1);for(e.stackName=await f("Enter your CloudFormation stack name?",n),p(o,e),d("BASE DOMAIN NAME"),r("Please enter the base domain name for your Medplum deployment."),r(""),r("Medplum deploys multiple subdomains for various services."),r(""),r('For example, "api." for the REST API and "app." for the web application.'),r("The base domain name is the common suffix for all subdomains."),r(""),r('For example, if your base domain name is "example.com",'),r('then the REST API will be "api.example.com".'),r(""),r('The base domain should include the TLD (i.e., ".com", ".org", ".net").'),r(""),r("Note that you must own the base domain, and it must use Route53 DNS.");!e.domainName;)e.domainName=await f("Enter your base domain name:");p(o,e),d("SUPPORT EMAIL"),r("Medplum sends transactional emails to users."),r("For example, emails to new users or for password reset."),r("Medplum will use the support email address to send these emails."),r("Note that you must verify the support email address in SES.");let a=await f("Enter your support email address:");d("API DOMAIN NAME"),r("Medplum deploys a REST API for the backend services."),e.apiDomainName=await f("Enter your REST API domain name:","api."+e.domainName),p(o,e),d("APP DOMAIN NAME"),r("Medplum deploys a web application for the user interface."),e.appDomainName=await f("Enter your web application domain name:","app."+e.domainName),p(o,e),d("STORAGE DOMAIN NAME"),r("Medplum deploys a storage service for file uploads."),e.storageDomainName=await f("Enter your storage domain name:","storage."+e.domainName),p(o,e),d("STORAGE BUCKET"),r("Medplum uses an S3 bucket to store binary content such as file uploads."),r("Medplum will create a the S3 bucket as part of the CloudFormation stack."),e.storageBucketName=await f("Enter your storage bucket name:","medplum-"+e.name+"-storage"),p(o,e),d("MAX AVAILABILITY ZONES"),r("Medplum API servers can be deployed in multiple availability zones."),r("This provides redundancy and high availability."),r("However, it also increases the cost of the deployment."),r("If you want to use all availability zones, choose a large number such as 99."),r("If you want to restrict the number, for example to manage EIP limits,"),r("then choose a small number such as 1 or 2."),e.maxAzs=await x("Enter the maximum number of availability zones:",[1,2,3,99],2),d("DATABASE INSTANCES"),r("Medplum uses a relational database to store data."),r("You can set up your own database,"),r("or Medplum can create a new RDS database as part of the CloudFormation stack."),await ee("Do you want to create a new RDS database as part of the CloudFormation stack?")?(r("Medplum will create a new RDS database as part of the CloudFormation stack."),r(""),r("If you need high availability, you can choose multiple instances."),r("Use 1 for a single instance, or 2 for a primary and a standby."),e.rdsInstances=await x("Enter the number of database instances:",[1,2],1)):(r("Medplum will not create a new RDS database."),r("Please create a new RDS database and enter the database name, username, and password."),r('Set the AWS Secrets Manager secret ARN in the config file in the "rdsSecretsArn" setting.'),e.rdsSecretsArn="TODO"),p(o,e),d("SERVER INSTANCES"),r("Medplum uses AWS Fargate to run the API servers."),r("Medplum will create a new Fargate cluster as part of the CloudFormation stack."),r("Fargate will automatically scale the number of servers up and down."),r("If you need high availability, you can choose multiple instances."),e.desiredServerCount=await x("Enter the number of server instances:",[1,2,3,4,6,8],1),p(o,e),d("SERVER MEMORY"),r("You can choose the amount of memory for each server instance."),r("The default is 512 MB, which is sufficient for getting started."),r("Note that only certain CPU units are compatible with memory units."),r('Consult AWS Fargate "Task Definition Parameters" for more information.'),e.serverMemory=await x("Enter the server memory (MB):",[512,1024,2048,4096,8192,16384],512),p(o,e),d("SERVER CPU"),r("You can choose the amount of CPU for each server instance."),r("CPU is expressed as an integer using AWS CPU units"),r("The default is 256, which is sufficient for getting started."),r("Note that only certain CPU units are compatible with memory units."),r('Consult AWS Fargate "Task Definition Parameters" for more information.'),e.serverCpu=await x("Enter the server CPU:",[256,512,1024,2048,4096,8192,16384],256),p(o,e),d("SERVER IMAGE"),r("Medplum uses Docker images for the API servers."),r("You can choose the image to use for the servers."),r("Docker images can be loaded from either Docker Hub or AWS ECR."),r("The default is the latest Medplum release."),e.serverImage=await f("Enter the server image:","medplum/medplum-server:latest"),p(o,e),d("SIGNING KEY"),r("Medplum uses AWS CloudFront Presigned URLs for binary content such as file uploads.");let{privateKey:i,publicKey:s,passphrase:c}=Lt();e.storagePublicKey=s,p(o,e),d("SSL CERTIFICATES"),r("Medplum will now check for existing SSL certificates for the subdomains.");let u=await Ft(e.region);r("Found "+u.length+" certificate(s).");for(let{region:z,certName:D}of[{region:e.region,certName:"api"},{region:"us-east-1",certName:"app"},{region:"us-east-1",certName:"storage"}]){r("");let N=await jt(e,u,z,D);e[Fe(D)]=N,p(o,e)}d("AWS PARAMETER STORE"),r("Medplum uses AWS Parameter Store to store sensitive configuration values."),r("These values will be encrypted at rest."),r(`The values will be stored in the "/medplum/${e.name}" path.`);let y={port:e.apiPort,baseUrl:`https://${e.apiDomainName}/`,appBaseUrl:`https://${e.appDomainName}/`,storageBaseUrl:`https://${e.storageDomainName}/binary/`,binaryStorage:`s3:${e.storageBucketName}`,signingKey:i,signingKeyPassphrase:c,supportEmail:a};r(JSON.stringify({...y,signingKey:"****",signingKeyPassphrase:"****"},null,2)),await U("Do you want to store these values in AWS Parameter Store?"),await _t(e.region,`/medplum/${e.name}/`,y),d("DONE!"),r("Medplum configuration complete."),r("You can now proceed to deploying the Medplum infrastructure with CDK."),r("Run:"),r(""),r(` npx cdk bootstrap -c config=${o}`),r(` npx cdk synth -c config=${o}`),e.region==="us-east-1"?r(` npx cdk deploy -c config=${o}`):r(` npx cdk deploy -c config=${o} --all`),r(""),r("See Medplum documentation for more information:"),r(""),r(" https://www.medplum.com/docs/self-hosting/install-on-aws"),r(""),F.close()}function r(e){F.write(e+`
|
|
3
|
+
`)}function d(e){r(`
|
|
4
|
+
`+e+`
|
|
5
|
+
`)}function f(e,t=""){return new Promise(o=>{F.question(e+(t?" ("+t+")":"")+" ",n=>{o(n||t.toString())})})}async function Q(e,t,o=""){let n=e+" ["+t.map(a=>a===o?"("+a+")":a).join("|")+"]";for(;;){let a=await f(n)||o;if(t.includes(a))return a;r("Please choose one of the following options: "+t.join(", "))}}async function x(e,t,o){return parseInt(await Q(e,t.map(n=>n.toString()),o.toString()),10)}async function ee(e){return(await Q(e,["y","n"])).toLowerCase()==="y"}async function U(e){if(!await ee(e))throw r("Exiting..."),new Error("User cancelled")}function p(e,t){(0,L.writeFileSync)((0,Be.resolve)(e),JSON.stringify(t,void 0,2),"utf-8")}async function Ut(e){try{let t=new j.STSClient({region:e}),o=new j.GetCallerIdentityCommand({});return(await t.send(o)).Account}catch(t){console.log("Warning: Unable to get AWS account ID",t.message);return}}async function Ft(e){let t=await Te(e);if(e!=="us-east-1"){let o=await Te("us-east-1");t.push(...o)}return t}async function Te(e){try{let t=new C.ACMClient({region:e}),o=new C.ListCertificatesCommand({MaxItems:1e3});return(await t.send(o)).CertificateSummaryList}catch(t){return console.log("Warning: Unable to list certificates",t.message),[]}}async function jt(e,t,o,n){let a=e[Bt(n)],i=t.find(c=>c.CertificateArn?.includes(o)&&c.DomainName===a);if(i)return r(`Found existing certificate for "${a}" in "${o}.`),i.CertificateArn;if(r(`No existing certificate found for "${a}" in "${o}.`),!await ee("Do you want to request a new certificate?"))return r(`Please add your certificate ARN to the config file in the "${Fe(n)}" setting.`),"TODO";let s=await $t(o,a);return r("Certificate ARN: "+s),s}async function $t(e,t){try{let o=await Q("Validate certificate using DNS or email validation?",["dns","email"],"dns"),n=new C.ACMClient({region:e}),a=new C.RequestCertificateCommand({DomainName:t,ValidationMethod:o.toUpperCase()});return(await n.send(a)).CertificateArn}catch(o){return console.log("Error: Unable to request certificate",o.message),"TODO"}}function Lt(){let e=(0,$.randomUUID)(),t=(0,$.generateKeyPairSync)("rsa",{modulusLength:2048,publicKeyEncoding:{type:"spki",format:"pem"},privateKeyEncoding:{type:"pkcs1",format:"pem",cipher:"aes-256-cbc",passphrase:e}});return{publicKey:t.publicKey,privateKey:t.privateKey,passphrase:e}}async function Ot(e,t){let o=new M.GetParameterCommand({Name:t,WithDecryption:!0});try{return(await e.send(o)).Parameter?.Value}catch(n){if(n.name==="ParameterNotFound")return;throw n}}async function Wt(e,t,o){let n=new M.PutParameterCommand({Name:t,Value:o,Type:"SecureString",Overwrite:!0});await e.send(n)}async function _t(e,t,o){let n=new M.SSMClient({region:e});for(let[a,i]of Object.entries(o)){let s=t+a,c=i.toString(),u=await Ot(n,s);u!==void 0&&u!==c&&(r(`Parameter "${s}" exists with different value.`),await U(`Do you want to overwrite "${s}"?`)),await Wt(n,s,c)}}async function $e(){let e=await J();for(let t of e){let o=t.StackName,n=await X(o);n&&(B(n),console.log(""))}}var qe=require("@aws-sdk/client-cloudfront"),ze=require("@aws-sdk/client-s3"),Ge=P(require("fast-glob")),g=require("fs"),ae=P(require("node-fetch")),He=require("os"),v=require("path"),Ve=require("stream/promises");var k=require("fs"),Le=require("path"),Oe=P(require("tar"));function S(e){console.log(JSON.stringify(e,null,2))}async function te(e,t,o){let n=ne(t.source);if(n)try{console.log("Update bot code.....");let a=await e.updateResource({...o,code:n});console.log(a?"Success! New bot version: "+a.meta?.versionId:"Bot not modified")}catch(a){console.log("Update error: ",a)}}async function We(e,t,o){let n=ne(t.dist??t.source);if(n)try{console.log("Deploying bot...");let a=await e.post(e.fhirUrl("Bot",o.id,"$deploy"),{code:n});console.log("Deploy result: "+a.issue?.[0]?.details?.text)}catch(a){console.log("Deploy error: ",a)}}async function oe(e,t){if(t.length<4){console.log("Error: command needs to be npx medplum <new-bot-name> <project-id> <source-file> <dist-file>");return}let o=t[0],n=t[1],a=t[2],i=t[3];try{let s={name:o,description:""},c=await e.post("admin/projects/"+n+"/bot",s),u=await e.readResource("Bot",c.id),y={name:o,id:c.id,source:a,dist:i};await te(e,y,u),console.log(`Success! Bot created: ${u.id}`),Kt(y)}catch(s){console.log("Error while creating new bot: "+s)}}function _e(e){let t=new RegExp("^"+qt(e).replace(/\\\*/g,".*")+"$"),o=O()?.bots?.filter(n=>t.test(n.name));return o||[]}function O(e){let t=e?`medplum.${e}.config.json`:"medplum.config.json",o=ne(t);if(o)return JSON.parse(o)}function ne(e){let t=(0,Le.resolve)(process.cwd(),e);return(0,k.existsSync)(t)?(0,k.readFileSync)(t,"utf8"):(console.log("Error: File does not exist: "+t),"")}function Kt(e){let t=O();t?.bots?.push(e),(0,k.writeFile)("medplum.config.json",JSON.stringify(t),()=>{console.log(`Bot added to config: ${e.id}`)})}function qt(e){return e.replace(/[/\-\\^$*+?.()|[\]{}]/g,"\\$&")}function Ke(e){let n=0,a=0;return Oe.default.x({cwd:e,filter:(i,s)=>{if(n++,n>100)throw new Error("Tar extractor reached max number of files");if(a+=s.size,a>10485760)throw new Error("Tar extractor reached max size");return!0}})}function re(){return{extension:[{url:"http://hl7.org/fhir/StructureDefinition/data-absent-reason",valueCode:"unsupported"}]}}async function Ye(e){let t=O(e);if(!t){console.log("Config not found");return}let o=await E(e);if(!o){console.log("Stack not found");return}let n=o.appBucket;if(!n){console.log("App bucket not found");return}let a=await Gt("@medplum/app","latest");Je(a,{MEDPLUM_BASE_URL:t.baseUrl,MEDPLUM_CLIENT_ID:t.clientId||"",GOOGLE_CLIENT_ID:t.googleClientId||"",RECAPTCHA_SITE_KEY:t.recaptchaSiteKey||"",MEDPLUM_REGISTER_ENABLED:t.registerEnabled?"true":"false"}),await Vt(a,n.PhysicalResourceId),o.appDistribution?.PhysicalResourceId&&await Xt(o.appDistribution.PhysicalResourceId),console.log("Done")}async function zt(e,t){let o=`https://registry.npmjs.org/${e}/${t}`;return(await(0,ae.default)(o)).json()}async function Gt(e,t){let n=(await zt(e,t)).dist.tarball,a=(0,g.mkdtempSync)((0,v.join)((0,He.tmpdir)(),"tarball-"));try{let i=await(0,ae.default)(n),s=Ke(a);return await(0,Ve.pipeline)(i.body,s),(0,v.join)(a,"package","dist")}catch(i){throw(0,g.rmSync)(a,{recursive:!0,force:!0}),i}}function Je(e,t){for(let o of(0,g.readdirSync)(e,{withFileTypes:!0})){let n=(0,v.join)(e,o.name);o.isDirectory()?Je(n,t):o.isFile()&&n.endsWith(".js")&&Ht(n,t)}}function Ht(e,t){let o=(0,g.readFileSync)(e,"utf-8");for(let[n,a]of Object.entries(t))o=o.replaceAll(`__${n}__`,a);(0,g.writeFileSync)(e,o)}async function Vt(e,t){let o=[["css/**/*.css","text/css",!0],["css/**/*.css.map","application/json",!0],["img/**/*.png","image/png",!0],["img/**/*.svg","image/svg+xml",!0],["js/**/*.js","application/javascript",!0],["js/**/*.js.map","application/json",!0],["js/**/*.txt","text/plain",!0],["favicon.ico","image/vnd.microsoft.icon",!0],["robots.txt","text/plain",!0],["workbox-*.js","application/javascript",!0],["workbox-*.js.map","application/json",!0],["manifest.webmanifest","application/manifest+json",!1],["service-worker.js","application/javascript",!1],["service-worker.js.map","application/json",!1],["index.html","text/html",!1]];for(let n of o)await Yt({rootDir:e,bucketName:t,fileNamePattern:n[0],contentType:n[1],cached:n[2]})}async function Yt(e){let t=Ge.default.sync(e.fileNamePattern,{cwd:e.rootDir});for(let o of t)await Jt((0,v.join)(e.rootDir,o),e)}async function Jt(e,t){let o=(0,g.createReadStream)(e),n=e.substring(t.rootDir.length+1).split(v.sep).join("/"),a={Bucket:t.bucketName,Key:n,Body:o,ContentType:t.contentType,CacheControl:t.cached?"public, max-age=31536000":"no-cache, no-store, must-revalidate"};console.log(`Uploading ${n} to ${t.bucketName}...`),await Ie.send(new ze.PutObjectCommand(a))}async function Xt(e){let t=await De.send(new qe.CreateInvalidationCommand({DistributionId:e,InvalidationBatch:{CallerReference:`invalidate-all-${Date.now()}`,Paths:{Quantity:1,Items:["/*"]}}}));console.log(`Created invalidation with ID: ${t.Invalidation?.Id}`)}var Xe=require("@aws-sdk/client-ecs");async function Ze(e){let t=await E(e);if(!t){console.log("Stack not found");return}let o=t.ecsCluster?.PhysicalResourceId;if(!o){console.log("ECS Cluster not found");return}let n=Z(t.ecsService);if(!n){console.log("ECS Service not found");return}await Ne.send(new Xe.UpdateServiceCommand({cluster:o,service:n,forceNewDeployment:!0})),console.log(`Service "${n}" updated successfully.`)}var b=new Qe.Command("aws").description("Commands to manage AWS resources");b.command("init").description("Initialize a new Medplum AWS CloudFormation stacks").action(je);b.command("list").description("List Medplum AWS CloudFormation stacks").action($e);b.command("describe").description("Describe a Medplum AWS CloudFormation stack by tag").argument("<tag>").action(Re);b.command("update-server").alias("deploy-server").description("Update the server image").argument("<tag>").action(Ze);b.command("update-app").alias("deploy-app").description("Update the app site").argument("<tag>").action(Ye);var et=require("commander");var tt=m("save"),ot=m("deploy"),nt=m("create"),rt=new et.Command("bot").addCommand(tt).addCommand(ot).addCommand(nt),ie=m("save-bot"),se=m("deploy-bot"),ce=m("create-bot");tt.description("Saving the bot").argument("<botName>").action(async(e,t)=>{let o=await l(t);await W(o,e)});ot.description("Deploy the app to AWS").argument("<botName>").action(async(e,t)=>{let o=await l(t);await W(o,e,!0)});nt.arguments("<botName> <projectId> <sourceFile> <distFile>").description("Creating a bot").action(async(e,t,o,n,a)=>{let i=await l(a);await oe(i,[e,t,o,n])});async function W(e,t,o=!1){let n=_e(t);for(let a of n){let i=await e.readResource("Bot",a.id);await te(e,a,i),o&&await We(e,a,i)}console.log(`Number of bots deployed: ${n.length}`)}ie.description("Saves the bot").argument("<botName>").action(async(e,t)=>{let o=await l(t);await W(o,e)});se.description("Deploy the bot to AWS").argument("<botName>").action(async(e,t)=>{let o=await l(t);await W(o,e,!0)});ce.arguments("<botName> <projectId> <sourceFile> <distFile>").description("Creates and saves the bot").action(async(e,t,o,n,a)=>{let i=await l(a);await oe(i,[e,t,o,n])});var it=require("commander"),_=require("fs"),me=require("path"),st=require("readline");var ct=m("export"),mt=m("import"),lt=new it.Command("bulk").addCommand(ct).addCommand(mt);ct.option("-e, --export-level <exportLevel>",'Optional export level. Defaults to system level export. "Group/:id" - Group of Patients, "Patient" - All Patients.').option("-t, --types <types>","optional resource types to export").option("-s, --since <since>","optional Resources will be included in the response if their state has changed after the supplied time (e.g. if Resource.meta.lastUpdated is later than the supplied _since time).").option("-d, --target-directory <targetDirectory>","optional target directory to save files from the bulk export operations.").action(async e=>{let{exportLevel:t,types:o,since:n,targetDirectory:a}=e,i=await l(e);(await i.bulkExport(t,o,n)).output?.forEach(async({type:c,url:u})=>{let y=new URL(u),z=await i.download(u),D=`${c}_${y.pathname}`.replace(/[^a-zA-Z0-9]+/g,"_")+".ndjson",N=(0,me.resolve)(a??"",D);(0,_.writeFile)(`${N}`,await z.text(),()=>{console.log(`${N} is created`)})})});mt.argument("<filename>","File Name").option("--num-resources-per-request <numResourcesPerRequest>","optional number of resources to import per batch request. Defaults to 25.","25").option("--add-extensions-for-missing-values","optional flag to add extensions for missing values in a resource",!1).option("-d, --target-directory <targetDirectory>","optional target directory of file to be imported").action(async(e,t)=>{let{numResourcesPerRequest:o,addExtensionsForMissingValues:n,targetDirectory:a}=t,i=(0,me.resolve)(a??process.cwd(),e),s=await l(t);await Zt(i,parseInt(o,10),s,n)});async function Zt(e,t,o,n){let a=[],i=(0,_.createReadStream)(e),s=(0,st.createInterface)({input:i});for await(let c of s){let u=Qt(c,n);a.push({resource:u,request:{method:"POST",url:u.resourceType}}),a.length%t===0&&(await at(a,o),a=[])}a.length>0&&await at(a,o)}async function at(e,t){(await t.executeBatch({resourceType:"Bundle",type:"transaction",entry:e})).entry?.forEach(n=>{S(n.response)})}function Qt(e,t){let o=JSON.parse(e);return t?eo(o):o}function eo(e){return e.resourceType==="ExplanationOfBenefit"?to(e):e}function to(e){return e.provider||(e.provider=re()),e.item?.forEach(t=>{t?.productOrService||(t.productOrService=re())}),e}var K=require("commander");var dt=m("list"),ut=m("current"),pt=m("switch"),ft=m("invite"),gt=new K.Command("project").addCommand(dt).addCommand(ut).addCommand(pt).addCommand(ft);dt.description("List of current projects").action(async e=>{let t=await l(e);oo(t)});function oo(e){let o=e.getLogins().map(n=>`${n.project.display} (${n.project.reference})`).join(`
|
|
3
6
|
|
|
4
|
-
|
|
5
|
-
var
|
|
6
|
-
var dotenv = require('dotenv');
|
|
7
|
-
var os = require('os');
|
|
8
|
-
var child_process = require('child_process');
|
|
9
|
-
var http = require('http');
|
|
10
|
-
var fs = require('fs');
|
|
11
|
-
var path = require('path');
|
|
12
|
-
var clientCloudformation = require('@aws-sdk/client-cloudformation');
|
|
13
|
-
var clientCloudfront = require('@aws-sdk/client-cloudfront');
|
|
14
|
-
var clientEcs = require('@aws-sdk/client-ecs');
|
|
15
|
-
var clientS3 = require('@aws-sdk/client-s3');
|
|
16
|
-
var fastGlob = require('fast-glob');
|
|
17
|
-
var fetch$1 = require('node-fetch');
|
|
18
|
-
var promises = require('stream/promises');
|
|
19
|
-
var tar = require('tar');
|
|
20
|
-
var clientAcm = require('@aws-sdk/client-acm');
|
|
21
|
-
var clientSsm = require('@aws-sdk/client-ssm');
|
|
22
|
-
var clientSts = require('@aws-sdk/client-sts');
|
|
23
|
-
var crypto = require('crypto');
|
|
24
|
-
var readline = require('readline');
|
|
25
|
-
|
|
26
|
-
function createMedplumCommand(name) {
|
|
27
|
-
return new commander.Command(name)
|
|
28
|
-
.option('--client-id <clientId>', 'FHIR server client id')
|
|
29
|
-
.option('--client-secret <clientSecret>', 'FHIR server client secret')
|
|
30
|
-
.option('--base-url <baseUrl>', 'FHIR server base url')
|
|
31
|
-
.option('--token-url <tokenUrl>', 'FHIR server token url')
|
|
32
|
-
.option('--authorize-url <authorizeUrl>', 'FHIR server authorize url')
|
|
33
|
-
.option('--fhir-url-path <fhirUrlPath>', 'FHIR server url path');
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
class FileSystemStorage extends core.ClientStorage {
|
|
37
|
-
constructor() {
|
|
38
|
-
super();
|
|
39
|
-
this.dirName = path.resolve(os.homedir(), '.medplum');
|
|
40
|
-
this.fileName = path.resolve(this.dirName, 'credentials');
|
|
41
|
-
}
|
|
42
|
-
clear() {
|
|
43
|
-
this.writeFile({});
|
|
44
|
-
}
|
|
45
|
-
getString(key) {
|
|
46
|
-
return this.readFile()?.[key];
|
|
47
|
-
}
|
|
48
|
-
setString(key, value) {
|
|
49
|
-
const data = this.readFile() || {};
|
|
50
|
-
if (value) {
|
|
51
|
-
data[key] = value;
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
delete data[key];
|
|
55
|
-
}
|
|
56
|
-
this.writeFile(data);
|
|
57
|
-
}
|
|
58
|
-
readFile() {
|
|
59
|
-
if (fs.existsSync(this.fileName)) {
|
|
60
|
-
return JSON.parse(fs.readFileSync(this.fileName, 'utf8'));
|
|
61
|
-
}
|
|
62
|
-
return undefined;
|
|
63
|
-
}
|
|
64
|
-
writeFile(data) {
|
|
65
|
-
if (!fs.existsSync(this.dirName)) {
|
|
66
|
-
fs.mkdirSync(this.dirName);
|
|
67
|
-
}
|
|
68
|
-
fs.writeFileSync(this.fileName, JSON.stringify(data, null, 2), 'utf8');
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
async function createMedplumClient(options) {
|
|
73
|
-
const baseUrl = options.baseUrl ?? process.env['MEDPLUM_BASE_URL'] ?? 'https://api.medplum.com/';
|
|
74
|
-
const fhirUrlPath = options.fhirUrlPath ?? process.env['MEDPLUM_FHIR_URL_PATH'] ?? '';
|
|
75
|
-
const accessToken = options.accessToken ?? process.env['MEDPLUM_CLIENT_ACCESS_TOKEN'] ?? '';
|
|
76
|
-
const tokenUrl = options.tokenUrl ?? process.env['MEDPLUM_TOKEN_URL'] ?? '';
|
|
77
|
-
const authorizeUrl = options.authorizeUrl ?? process.env['MEDPLUM_AUTHORIZE_URL'] ?? '';
|
|
78
|
-
const fetchApi = options.fetch ?? fetch;
|
|
79
|
-
const medplumClient = new core.MedplumClient({
|
|
80
|
-
fetch: fetchApi,
|
|
81
|
-
baseUrl,
|
|
82
|
-
tokenUrl,
|
|
83
|
-
fhirUrlPath,
|
|
84
|
-
authorizeUrl,
|
|
85
|
-
storage: new FileSystemStorage(),
|
|
86
|
-
onUnauthenticated: onUnauthenticated,
|
|
87
|
-
});
|
|
88
|
-
if (accessToken) {
|
|
89
|
-
medplumClient.setAccessToken(accessToken);
|
|
90
|
-
}
|
|
91
|
-
const clientId = options.clientId || process.env['MEDPLUM_CLIENT_ID'];
|
|
92
|
-
const clientSecret = options.clientSecret || process.env['MEDPLUM_CLIENT_SECRET'];
|
|
93
|
-
if (clientId && clientSecret) {
|
|
94
|
-
medplumClient.setBasicAuth(clientId, clientSecret);
|
|
95
|
-
await medplumClient.startClientLogin(clientId, clientSecret);
|
|
96
|
-
}
|
|
97
|
-
return medplumClient;
|
|
98
|
-
}
|
|
99
|
-
function onUnauthenticated() {
|
|
100
|
-
console.log('Unauthenticated: run `npx medplum login` to sign in');
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const clientId = 'medplum-cli';
|
|
104
|
-
const redirectUri = 'http://localhost:9615';
|
|
105
|
-
const login = createMedplumCommand('login');
|
|
106
|
-
const whoami = createMedplumCommand('whoami');
|
|
107
|
-
login.action(async (options) => {
|
|
108
|
-
const medplum = await createMedplumClient(options);
|
|
109
|
-
await startLogin(medplum);
|
|
110
|
-
});
|
|
111
|
-
whoami.action(async (options) => {
|
|
112
|
-
const medplum = await createMedplumClient(options);
|
|
113
|
-
printMe(medplum);
|
|
114
|
-
});
|
|
115
|
-
async function startLogin(medplum) {
|
|
116
|
-
await startWebServer(medplum);
|
|
117
|
-
const loginUrl = new URL(medplum.getAuthorizeUrl());
|
|
118
|
-
loginUrl.searchParams.set('client_id', clientId);
|
|
119
|
-
loginUrl.searchParams.set('redirect_uri', redirectUri);
|
|
120
|
-
loginUrl.searchParams.set('scope', 'openid');
|
|
121
|
-
loginUrl.searchParams.set('response_type', 'code');
|
|
122
|
-
loginUrl.searchParams.set('prompt', 'login');
|
|
123
|
-
await openBrowser(loginUrl.toString());
|
|
124
|
-
}
|
|
125
|
-
async function startWebServer(medplum) {
|
|
126
|
-
const server = http.createServer(async (req, res) => {
|
|
127
|
-
const url = new URL(req.url, 'http://localhost:9615');
|
|
128
|
-
const code = url.searchParams.get('code');
|
|
129
|
-
if (url.pathname === '/' && code) {
|
|
130
|
-
try {
|
|
131
|
-
const profile = await medplum.processCode(code, { clientId, redirectUri });
|
|
132
|
-
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
|
133
|
-
res.end(`Signed in as ${core.getDisplayString(profile)}. You may close this window.`);
|
|
134
|
-
}
|
|
135
|
-
catch (err) {
|
|
136
|
-
res.writeHead(400, { 'Content-Type': 'text/plain' });
|
|
137
|
-
res.end(`Error: ${core.normalizeErrorString(err)}`);
|
|
138
|
-
}
|
|
139
|
-
finally {
|
|
140
|
-
server.close();
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
|
145
|
-
res.end('Not found');
|
|
146
|
-
}
|
|
147
|
-
}).listen(9615);
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Opens a web browser to the specified URL.
|
|
151
|
-
* See: https://hasinthaindrajee.medium.com/browser-sso-for-cli-applications-b0be743fa656
|
|
152
|
-
* @param url The URL to open.
|
|
153
|
-
*/
|
|
154
|
-
async function openBrowser(url) {
|
|
155
|
-
const os$1 = os.platform();
|
|
156
|
-
let cmd = undefined;
|
|
157
|
-
switch (os$1) {
|
|
158
|
-
case 'openbsd':
|
|
159
|
-
case 'linux':
|
|
160
|
-
cmd = `xdg-open '${url}'`;
|
|
161
|
-
break;
|
|
162
|
-
case 'darwin':
|
|
163
|
-
cmd = `open '${url}'`;
|
|
164
|
-
break;
|
|
165
|
-
case 'win32':
|
|
166
|
-
cmd = `cmd /c start "" "${url}"`;
|
|
167
|
-
break;
|
|
168
|
-
default:
|
|
169
|
-
throw new Error('Unsupported platform: ' + os$1);
|
|
170
|
-
}
|
|
171
|
-
child_process.exec(cmd);
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* Prints the current user and project.
|
|
175
|
-
* @param medplum The Medplum client.
|
|
176
|
-
*/
|
|
177
|
-
function printMe(medplum) {
|
|
178
|
-
const loginState = medplum.getActiveLogin();
|
|
179
|
-
if (loginState) {
|
|
180
|
-
console.log(`Server: ${medplum.getBaseUrl()}`);
|
|
181
|
-
console.log(`Profile: ${loginState.profile.display} (${loginState.profile.reference})`);
|
|
182
|
-
console.log(`Project: ${loginState.project.display} (${loginState.project.reference})`);
|
|
183
|
-
}
|
|
184
|
-
else {
|
|
185
|
-
console.log('Not logged in');
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
const cloudFormationClient = new clientCloudformation.CloudFormationClient({});
|
|
190
|
-
const cloudFrontClient = new clientCloudfront.CloudFrontClient({});
|
|
191
|
-
const ecsClient = new clientEcs.ECSClient({});
|
|
192
|
-
const s3Client = new clientS3.S3Client({});
|
|
193
|
-
const tagKey = 'medplum:environment';
|
|
194
|
-
/**
|
|
195
|
-
* Returns a list of all AWS CloudFormation stacks (both Medplum and non-Medplum).
|
|
196
|
-
* @returns List of AWS CloudFormation stacks.
|
|
197
|
-
*/
|
|
198
|
-
async function getAllStacks() {
|
|
199
|
-
const listResult = await cloudFormationClient.send(new clientCloudformation.ListStacksCommand({}));
|
|
200
|
-
return (listResult.StackSummaries?.filter((s) => s.StackName && s.StackStatus !== 'DELETE_COMPLETE') || []);
|
|
201
|
-
}
|
|
202
|
-
/**
|
|
203
|
-
* Returns Medplum stack details for the given tag.
|
|
204
|
-
* @param tag The Medplum stack tag.
|
|
205
|
-
* @returns The Medplum stack details.
|
|
206
|
-
*/
|
|
207
|
-
async function getStackByTag(tag) {
|
|
208
|
-
const stackSummaries = await getAllStacks();
|
|
209
|
-
for (const stackSummary of stackSummaries) {
|
|
210
|
-
const stackName = stackSummary.StackName;
|
|
211
|
-
const details = await getStackDetails(stackName);
|
|
212
|
-
if (details?.tag === tag) {
|
|
213
|
-
return details;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
return undefined;
|
|
217
|
-
}
|
|
218
|
-
/**
|
|
219
|
-
* Returns Medplum stack details for the given stack name.
|
|
220
|
-
* @param stackName The CloudFormation stack name.
|
|
221
|
-
* @returns The Medplum stack details.
|
|
222
|
-
*/
|
|
223
|
-
async function getStackDetails(stackName) {
|
|
224
|
-
const describeStacksCommand = new clientCloudformation.DescribeStacksCommand({ StackName: stackName });
|
|
225
|
-
const stackDetails = await cloudFormationClient.send(describeStacksCommand);
|
|
226
|
-
const stack = stackDetails?.Stacks?.[0];
|
|
227
|
-
const medplumTag = stack?.Tags?.find((tag) => tag.Key === tagKey);
|
|
228
|
-
if (!medplumTag) {
|
|
229
|
-
return undefined;
|
|
230
|
-
}
|
|
231
|
-
const stackResources = await cloudFormationClient.send(new clientCloudformation.DescribeStackResourcesCommand({ StackName: stackName }));
|
|
232
|
-
if (!stackResources.StackResources) {
|
|
233
|
-
return undefined;
|
|
234
|
-
}
|
|
235
|
-
const result = {
|
|
236
|
-
stack: stack,
|
|
237
|
-
tag: medplumTag.Value,
|
|
238
|
-
};
|
|
239
|
-
for (const resource of stackResources.StackResources) {
|
|
240
|
-
if (resource.ResourceType === 'AWS::ECS::Cluster') {
|
|
241
|
-
result.ecsCluster = resource;
|
|
242
|
-
}
|
|
243
|
-
else if (resource.ResourceType === 'AWS::ECS::Service') {
|
|
244
|
-
result.ecsService = resource;
|
|
245
|
-
}
|
|
246
|
-
else if (resource.ResourceType === 'AWS::S3::Bucket' &&
|
|
247
|
-
resource.LogicalResourceId?.startsWith('FrontEndAppBucket')) {
|
|
248
|
-
result.appBucket = resource;
|
|
249
|
-
}
|
|
250
|
-
else if (resource.ResourceType === 'AWS::S3::Bucket' &&
|
|
251
|
-
resource.LogicalResourceId?.startsWith('StorageStorageBucket')) {
|
|
252
|
-
result.storageBucket = resource;
|
|
253
|
-
}
|
|
254
|
-
else if (resource.ResourceType === 'AWS::CloudFront::Distribution' &&
|
|
255
|
-
resource.LogicalResourceId?.startsWith('FrontEndAppDistribution')) {
|
|
256
|
-
result.appDistribution = resource;
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
return result;
|
|
260
|
-
}
|
|
261
|
-
/**
|
|
262
|
-
* Prints the given Medplum stack details to stdout.
|
|
263
|
-
* @param details The Medplum stack details.
|
|
264
|
-
*/
|
|
265
|
-
function printStackDetails(details) {
|
|
266
|
-
console.log(`Medplum Tag: ${details.tag}`);
|
|
267
|
-
console.log(`Stack Name: ${details.stack.StackName}`);
|
|
268
|
-
console.log(`Stack ID: ${details.stack.StackId}`);
|
|
269
|
-
console.log(`Status: ${details.stack.StackStatus}`);
|
|
270
|
-
console.log(`ECS Cluster: ${details.ecsCluster?.PhysicalResourceId}`);
|
|
271
|
-
console.log(`ECS Service: ${getEcsServiceName(details.ecsService)}`);
|
|
272
|
-
console.log(`App Bucket: ${details.appBucket?.PhysicalResourceId}`);
|
|
273
|
-
console.log(`Storage Bucket: ${details.storageBucket?.PhysicalResourceId}`);
|
|
274
|
-
}
|
|
275
|
-
/**
|
|
276
|
-
* Parses the ECS service name from the given AWS ECS service resource.
|
|
277
|
-
* @param resource The AWS ECS service resource.
|
|
278
|
-
* @returns The ECS service name.
|
|
279
|
-
*/
|
|
280
|
-
function getEcsServiceName(resource) {
|
|
281
|
-
return resource?.PhysicalResourceId?.split('/')?.pop() || '';
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
/**
|
|
285
|
-
* The AWS "describe" command prints details about a Medplum CloudFormation stack.
|
|
286
|
-
* @param tag The Medplum stack tag.
|
|
287
|
-
*/
|
|
288
|
-
async function describeStacksCommand(tag) {
|
|
289
|
-
const details = await getStackByTag(tag);
|
|
290
|
-
if (!details) {
|
|
291
|
-
console.log('Stack not found');
|
|
292
|
-
return;
|
|
293
|
-
}
|
|
294
|
-
printStackDetails(details);
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
/**
|
|
298
|
-
* The AWS "list" command prints summary details about all Medplum CloudFormation stacks.
|
|
299
|
-
*/
|
|
300
|
-
async function listStacksCommand() {
|
|
301
|
-
const stackSummaries = await getAllStacks();
|
|
302
|
-
for (const stackSummary of stackSummaries) {
|
|
303
|
-
const stackName = stackSummary.StackName;
|
|
304
|
-
const details = await getStackDetails(stackName);
|
|
305
|
-
if (!details) {
|
|
306
|
-
continue;
|
|
307
|
-
}
|
|
308
|
-
printStackDetails(details);
|
|
309
|
-
console.log('');
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
function prettyPrint(input) {
|
|
314
|
-
console.log(JSON.stringify(input, null, 2));
|
|
315
|
-
}
|
|
316
|
-
async function saveBot(medplum, botConfig, bot) {
|
|
317
|
-
const code = readFileContents(botConfig.source);
|
|
318
|
-
if (!code) {
|
|
319
|
-
return;
|
|
320
|
-
}
|
|
321
|
-
try {
|
|
322
|
-
console.log('Update bot code.....');
|
|
323
|
-
const updateResult = await medplum.updateResource({
|
|
324
|
-
...bot,
|
|
325
|
-
code,
|
|
326
|
-
});
|
|
327
|
-
if (!updateResult) {
|
|
328
|
-
console.log('Bot not modified');
|
|
329
|
-
}
|
|
330
|
-
else {
|
|
331
|
-
console.log('Success! New bot version: ' + updateResult.meta?.versionId);
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
catch (err) {
|
|
335
|
-
console.log('Update error: ', err);
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
async function deployBot(medplum, botConfig, bot) {
|
|
339
|
-
const code = readFileContents(botConfig.dist ?? botConfig.source);
|
|
340
|
-
if (!code) {
|
|
341
|
-
return;
|
|
342
|
-
}
|
|
343
|
-
try {
|
|
344
|
-
console.log('Deploying bot...');
|
|
345
|
-
const deployResult = (await medplum.post(medplum.fhirUrl('Bot', bot.id, '$deploy'), {
|
|
346
|
-
code,
|
|
347
|
-
}));
|
|
348
|
-
console.log('Deploy result: ' + deployResult.issue?.[0]?.details?.text);
|
|
349
|
-
}
|
|
350
|
-
catch (err) {
|
|
351
|
-
console.log('Deploy error: ', err);
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
async function createBot(medplum, argv) {
|
|
355
|
-
if (argv.length < 4) {
|
|
356
|
-
console.log(`Error: command needs to be npx medplum <new-bot-name> <project-id> <source-file> <dist-file>`);
|
|
357
|
-
return;
|
|
358
|
-
}
|
|
359
|
-
const botName = argv[0];
|
|
360
|
-
const projectId = argv[1];
|
|
361
|
-
const sourceFile = argv[2];
|
|
362
|
-
const distFile = argv[3];
|
|
363
|
-
try {
|
|
364
|
-
const body = {
|
|
365
|
-
name: botName,
|
|
366
|
-
description: '',
|
|
367
|
-
};
|
|
368
|
-
const newBot = await medplum.post('admin/projects/' + projectId + '/bot', body);
|
|
369
|
-
const bot = await medplum.readResource('Bot', newBot.id);
|
|
370
|
-
const botConfig = {
|
|
371
|
-
name: botName,
|
|
372
|
-
id: newBot.id,
|
|
373
|
-
source: sourceFile,
|
|
374
|
-
dist: distFile,
|
|
375
|
-
};
|
|
376
|
-
await saveBot(medplum, botConfig, bot);
|
|
377
|
-
console.log(`Success! Bot created: ${bot.id}`);
|
|
378
|
-
addBotToConfig(botConfig);
|
|
379
|
-
}
|
|
380
|
-
catch (err) {
|
|
381
|
-
console.log('Error while creating new bot: ' + err);
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
function readBotConfigs(botName) {
|
|
385
|
-
const regExBotName = new RegExp('^' + escapeRegex(botName).replace(/\\\*/g, '.*') + '$');
|
|
386
|
-
const botConfigs = readConfig()?.bots?.filter((b) => regExBotName.test(b.name));
|
|
387
|
-
if (!botConfigs) {
|
|
388
|
-
return [];
|
|
389
|
-
}
|
|
390
|
-
return botConfigs;
|
|
391
|
-
}
|
|
392
|
-
function readConfig(tagName) {
|
|
393
|
-
const fileName = tagName ? `medplum.${tagName}.config.json` : 'medplum.config.json';
|
|
394
|
-
const content = readFileContents(fileName);
|
|
395
|
-
if (!content) {
|
|
396
|
-
return undefined;
|
|
397
|
-
}
|
|
398
|
-
return JSON.parse(content);
|
|
399
|
-
}
|
|
400
|
-
function readFileContents(fileName) {
|
|
401
|
-
const path$1 = path.resolve(process.cwd(), fileName);
|
|
402
|
-
if (!fs.existsSync(path$1)) {
|
|
403
|
-
console.log('Error: File does not exist: ' + path$1);
|
|
404
|
-
return '';
|
|
405
|
-
}
|
|
406
|
-
return fs.readFileSync(path$1, 'utf8');
|
|
407
|
-
}
|
|
408
|
-
function addBotToConfig(botConfig) {
|
|
409
|
-
const config = readConfig();
|
|
410
|
-
config?.bots?.push(botConfig);
|
|
411
|
-
fs.writeFile('medplum.config.json', JSON.stringify(config), () => {
|
|
412
|
-
console.log(`Bot added to config: ${botConfig.id}`);
|
|
413
|
-
});
|
|
414
|
-
}
|
|
415
|
-
function escapeRegex(str) {
|
|
416
|
-
return str.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
417
|
-
}
|
|
418
|
-
/**
|
|
419
|
-
* Creates a safe tar extractor that limits the number of files and total size.
|
|
420
|
-
*
|
|
421
|
-
* Expanding archive files without controlling resource consumption is security-sensitive
|
|
422
|
-
*
|
|
423
|
-
* See: https://sonarcloud.io/organizations/medplum/rules?open=typescript%3AS5042&rule_key=typescript%3AS5042
|
|
424
|
-
* @param destinationDir The destination directory where all files will be extracted.
|
|
425
|
-
* @returns A tar file extractor.
|
|
426
|
-
*/
|
|
427
|
-
function safeTarExtractor(destinationDir) {
|
|
428
|
-
const MAX_FILES = 100;
|
|
429
|
-
const MAX_SIZE = 10 * 1024 * 1024; // 10 MB
|
|
430
|
-
let fileCount = 0;
|
|
431
|
-
let totalSize = 0;
|
|
432
|
-
return tar.x({
|
|
433
|
-
cwd: destinationDir,
|
|
434
|
-
filter: (_path, entry) => {
|
|
435
|
-
fileCount++;
|
|
436
|
-
if (fileCount > MAX_FILES) {
|
|
437
|
-
throw new Error('Tar extractor reached max number of files');
|
|
438
|
-
}
|
|
439
|
-
totalSize += entry.size;
|
|
440
|
-
if (totalSize > MAX_SIZE) {
|
|
441
|
-
throw new Error('Tar extractor reached max size');
|
|
442
|
-
}
|
|
443
|
-
return true;
|
|
444
|
-
},
|
|
445
|
-
});
|
|
446
|
-
}
|
|
447
|
-
function getUnsupportedExtension() {
|
|
448
|
-
return {
|
|
449
|
-
extension: [
|
|
450
|
-
{
|
|
451
|
-
url: 'http://hl7.org/fhir/StructureDefinition/data-absent-reason',
|
|
452
|
-
valueCode: 'unsupported',
|
|
453
|
-
},
|
|
454
|
-
],
|
|
455
|
-
};
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
/**
|
|
459
|
-
* The AWS "update-app" command updates the Medplum app in a Medplum CloudFormation stack to the latest version.
|
|
460
|
-
* @param tag The Medplum stack tag.
|
|
461
|
-
*/
|
|
462
|
-
async function updateAppCommand(tag) {
|
|
463
|
-
const config = readConfig(tag);
|
|
464
|
-
if (!config) {
|
|
465
|
-
console.log('Config not found');
|
|
466
|
-
return;
|
|
467
|
-
}
|
|
468
|
-
const details = await getStackByTag(tag);
|
|
469
|
-
if (!details) {
|
|
470
|
-
console.log('Stack not found');
|
|
471
|
-
return;
|
|
472
|
-
}
|
|
473
|
-
const appBucket = details.appBucket;
|
|
474
|
-
if (!appBucket) {
|
|
475
|
-
console.log('App bucket not found');
|
|
476
|
-
return;
|
|
477
|
-
}
|
|
478
|
-
const tmpDir = await downloadNpmPackage('@medplum/app', 'latest');
|
|
479
|
-
// Replace variables in the app
|
|
480
|
-
replaceVariables(tmpDir, {
|
|
481
|
-
MEDPLUM_BASE_URL: config.baseUrl,
|
|
482
|
-
MEDPLUM_CLIENT_ID: config.clientId || '',
|
|
483
|
-
GOOGLE_CLIENT_ID: config.googleClientId || '',
|
|
484
|
-
RECAPTCHA_SITE_KEY: config.recaptchaSiteKey || '',
|
|
485
|
-
MEDPLUM_REGISTER_ENABLED: config.registerEnabled ? 'true' : 'false',
|
|
486
|
-
});
|
|
487
|
-
// Upload the app to S3 with correct content-type and cache-control
|
|
488
|
-
await uploadAppToS3(tmpDir, appBucket.PhysicalResourceId);
|
|
489
|
-
// Create a CloudFront invalidation to clear any cached resources
|
|
490
|
-
if (details.appDistribution?.PhysicalResourceId) {
|
|
491
|
-
await createInvalidation(details.appDistribution.PhysicalResourceId);
|
|
492
|
-
}
|
|
493
|
-
console.log('Done');
|
|
494
|
-
}
|
|
495
|
-
/**
|
|
496
|
-
* Returns NPM package metadata for a given package name.
|
|
497
|
-
* See: https://github.com/npm/registry/blob/master/docs/REGISTRY-API.md#getpackageversion
|
|
498
|
-
* @param packageName The npm package name.
|
|
499
|
-
* @param version The npm package version string.
|
|
500
|
-
* @returns The package.json metadata content.
|
|
501
|
-
*/
|
|
502
|
-
async function getNpmPackageMetadata(packageName, version) {
|
|
503
|
-
const url = `https://registry.npmjs.org/${packageName}/${version}`;
|
|
504
|
-
const response = await fetch$1(url);
|
|
505
|
-
return response.json();
|
|
506
|
-
}
|
|
507
|
-
/**
|
|
508
|
-
* Downloads and extracts an NPM package.
|
|
509
|
-
* @param packageName The NPM package name.
|
|
510
|
-
* @param version The NPM package version or "latest".
|
|
511
|
-
* @returns Path to temporary directory where the package was downloaded and extracted.
|
|
512
|
-
*/
|
|
513
|
-
async function downloadNpmPackage(packageName, version) {
|
|
514
|
-
const packageMetadata = await getNpmPackageMetadata(packageName, version);
|
|
515
|
-
const tarballUrl = packageMetadata.dist.tarball;
|
|
516
|
-
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'tarball-'));
|
|
517
|
-
try {
|
|
518
|
-
const response = await fetch$1(tarballUrl);
|
|
519
|
-
const extractor = safeTarExtractor(tmpDir);
|
|
520
|
-
await promises.pipeline(response.body, extractor);
|
|
521
|
-
return path.join(tmpDir, 'package', 'dist');
|
|
522
|
-
}
|
|
523
|
-
catch (error) {
|
|
524
|
-
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
525
|
-
throw error;
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
/**
|
|
529
|
-
* Replaces variables in all JS files in the given folder.
|
|
530
|
-
* @param folderName The folder name of the files.
|
|
531
|
-
* @param replacements The collection of variable placeholders and replacements.
|
|
532
|
-
*/
|
|
533
|
-
function replaceVariables(folderName, replacements) {
|
|
534
|
-
for (const item of fs.readdirSync(folderName, { withFileTypes: true })) {
|
|
535
|
-
const itemPath = path.join(folderName, item.name);
|
|
536
|
-
if (item.isDirectory()) {
|
|
537
|
-
replaceVariables(itemPath, replacements);
|
|
538
|
-
}
|
|
539
|
-
else if (item.isFile() && itemPath.endsWith('.js')) {
|
|
540
|
-
replaceVariablesInFile(itemPath, replacements);
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
/**
|
|
545
|
-
* Replaces variables in the JS file.
|
|
546
|
-
* @param fileName The file name.
|
|
547
|
-
* @param replacements The collection of variable placeholders and replacements.
|
|
548
|
-
*/
|
|
549
|
-
function replaceVariablesInFile(fileName, replacements) {
|
|
550
|
-
let contents = fs.readFileSync(fileName, 'utf-8');
|
|
551
|
-
for (const [placeholder, replacement] of Object.entries(replacements)) {
|
|
552
|
-
contents = contents.replaceAll(`process.env.${placeholder}`, `'${replacement}'`);
|
|
553
|
-
}
|
|
554
|
-
fs.writeFileSync(fileName, contents);
|
|
555
|
-
}
|
|
556
|
-
/**
|
|
557
|
-
* Uploads the app to S3.
|
|
558
|
-
* Ensures correct content-type and cache-control for each file.
|
|
559
|
-
* @param tmpDir The temporary directory where the app is located.
|
|
560
|
-
* @param bucketName The destination S3 bucket name.
|
|
561
|
-
*/
|
|
562
|
-
async function uploadAppToS3(tmpDir, bucketName) {
|
|
563
|
-
// Manually iterate and upload files
|
|
564
|
-
// Automatic content-type detection is not reliable on Microsoft Windows
|
|
565
|
-
// So we explicitly set content-type
|
|
566
|
-
const uploadPatterns = [
|
|
567
|
-
// Cached
|
|
568
|
-
// These files generally have a hash, so they can be cached forever
|
|
569
|
-
// It is important to upload them first to avoid broken references from index.html
|
|
570
|
-
['css/**/*.css', 'text/css', true],
|
|
571
|
-
['css/**/*.css.map', 'application/json', true],
|
|
572
|
-
['img/**/*.png', 'image/png', true],
|
|
573
|
-
['img/**/*.svg', 'image/svg+xml', true],
|
|
574
|
-
['js/**/*.js', 'application/javascript', true],
|
|
575
|
-
['js/**/*.js.map', 'application/json', true],
|
|
576
|
-
['js/**/*.txt', 'text/plain', true],
|
|
577
|
-
['favicon.ico', 'image/vnd.microsoft.icon', true],
|
|
578
|
-
['robots.txt', 'text/plain', true],
|
|
579
|
-
['workbox-*.js', 'application/javascript', true],
|
|
580
|
-
['workbox-*.js.map', 'application/json', true],
|
|
581
|
-
// Not cached
|
|
582
|
-
['manifest.webmanifest', 'application/manifest+json', false],
|
|
583
|
-
['service-worker.js', 'application/javascript', false],
|
|
584
|
-
['service-worker.js.map', 'application/json', false],
|
|
585
|
-
['index.html', 'text/html', false],
|
|
586
|
-
];
|
|
587
|
-
for (const uploadPattern of uploadPatterns) {
|
|
588
|
-
await uploadFolderToS3({
|
|
589
|
-
rootDir: tmpDir,
|
|
590
|
-
bucketName,
|
|
591
|
-
fileNamePattern: uploadPattern[0],
|
|
592
|
-
contentType: uploadPattern[1],
|
|
593
|
-
cached: uploadPattern[2],
|
|
594
|
-
});
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
/**
|
|
598
|
-
* Uploads a directory of files to S3.
|
|
599
|
-
* @param options The upload options such as bucket name, content type, and cache control.
|
|
600
|
-
* @param options.rootDir The root directory of the upload.
|
|
601
|
-
* @param options.bucketName The destination bucket name.
|
|
602
|
-
* @param options.fileNamePattern The glob file pattern to upload.
|
|
603
|
-
* @param options.contentType The content type MIME type.
|
|
604
|
-
* @param options.cached True to mark as public and cached forever.
|
|
605
|
-
*/
|
|
606
|
-
async function uploadFolderToS3(options) {
|
|
607
|
-
const items = fastGlob.sync(options.fileNamePattern, { cwd: options.rootDir });
|
|
608
|
-
for (const item of items) {
|
|
609
|
-
await uploadFileToS3(path.join(options.rootDir, item), options);
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
/**
|
|
613
|
-
* Uploads a file to S3.
|
|
614
|
-
* @param filePath The file path.
|
|
615
|
-
* @param options The upload options such as bucket name, content type, and cache control.
|
|
616
|
-
* @param options.rootDir The root directory of the upload.
|
|
617
|
-
* @param options.bucketName The destination bucket name.
|
|
618
|
-
* @param options.contentType The content type MIME type.
|
|
619
|
-
* @param options.cached True to mark as public and cached forever.
|
|
620
|
-
*/
|
|
621
|
-
async function uploadFileToS3(filePath, options) {
|
|
622
|
-
const fileStream = fs.createReadStream(filePath);
|
|
623
|
-
const s3Key = filePath
|
|
624
|
-
.substring(options.rootDir.length + 1)
|
|
625
|
-
.split(path.sep)
|
|
626
|
-
.join('/');
|
|
627
|
-
const putObjectParams = {
|
|
628
|
-
Bucket: options.bucketName,
|
|
629
|
-
Key: s3Key,
|
|
630
|
-
Body: fileStream,
|
|
631
|
-
ContentType: options.contentType,
|
|
632
|
-
CacheControl: options.cached ? 'public, max-age=31536000' : 'no-cache, no-store, must-revalidate',
|
|
633
|
-
};
|
|
634
|
-
console.log(`Uploading ${s3Key} to ${options.bucketName}...`);
|
|
635
|
-
await s3Client.send(new clientS3.PutObjectCommand(putObjectParams));
|
|
636
|
-
}
|
|
637
|
-
/**
|
|
638
|
-
* Creates a CloudFront invalidation to clear the cache for all files.
|
|
639
|
-
* This is not strictly necessary, but it helps to ensure that the latest version of the app is served.
|
|
640
|
-
* In a perfect world, every deploy is clean, and hashed resources should be cached forever.
|
|
641
|
-
* However, we do not recalculate hashes after variable replacements.
|
|
642
|
-
* So if variables change, we need to invalidate the cache.
|
|
643
|
-
* @param distributionId The CloudFront distribution ID.
|
|
644
|
-
*/
|
|
645
|
-
async function createInvalidation(distributionId) {
|
|
646
|
-
const response = await cloudFrontClient.send(new clientCloudfront.CreateInvalidationCommand({
|
|
647
|
-
DistributionId: distributionId,
|
|
648
|
-
InvalidationBatch: {
|
|
649
|
-
CallerReference: `invalidate-all-${Date.now()}`,
|
|
650
|
-
Paths: {
|
|
651
|
-
Quantity: 1,
|
|
652
|
-
Items: ['/*'],
|
|
653
|
-
},
|
|
654
|
-
},
|
|
655
|
-
}));
|
|
656
|
-
console.log(`Created invalidation with ID: ${response.Invalidation?.Id}`);
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
/**
|
|
660
|
-
* The AWS "update-server" command updates the Medplum server in a Medplum CloudFormation stack.
|
|
661
|
-
* @param tag The Medplum stack tag.
|
|
662
|
-
*/
|
|
663
|
-
async function updateServerCommand(tag) {
|
|
664
|
-
const details = await getStackByTag(tag);
|
|
665
|
-
if (!details) {
|
|
666
|
-
console.log('Stack not found');
|
|
667
|
-
return;
|
|
668
|
-
}
|
|
669
|
-
const ecsCluster = details.ecsCluster?.PhysicalResourceId;
|
|
670
|
-
if (!ecsCluster) {
|
|
671
|
-
console.log('ECS Cluster not found');
|
|
672
|
-
return;
|
|
673
|
-
}
|
|
674
|
-
const ecsService = getEcsServiceName(details.ecsService);
|
|
675
|
-
if (!ecsService) {
|
|
676
|
-
console.log('ECS Service not found');
|
|
677
|
-
return;
|
|
678
|
-
}
|
|
679
|
-
await ecsClient.send(new clientEcs.UpdateServiceCommand({
|
|
680
|
-
cluster: ecsCluster,
|
|
681
|
-
service: ecsService,
|
|
682
|
-
forceNewDeployment: true,
|
|
683
|
-
}));
|
|
684
|
-
console.log(`Service "${ecsService}" updated successfully.`);
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
const getDomainSetting = (domain) => `${domain}DomainName`;
|
|
688
|
-
const getDomainCertSetting = (domain) => `${domain}SslCertArn`;
|
|
689
|
-
let terminal;
|
|
690
|
-
async function initStackCommand() {
|
|
691
|
-
const config = { apiPort: 8103, region: 'us-east-1' };
|
|
692
|
-
terminal = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
693
|
-
header('MEDPLUM');
|
|
694
|
-
print('This tool prepares the necessary prerequisites for deploying Medplum in your AWS account.');
|
|
695
|
-
print('');
|
|
696
|
-
print('Most Medplum infrastructure is deployed using the AWS CDK.');
|
|
697
|
-
print('However, some AWS resources must be created manually, such as email addresses and SSL certificates.');
|
|
698
|
-
print('This tool will help you create those resources.');
|
|
699
|
-
print('');
|
|
700
|
-
print('Upon completion, this tool will:');
|
|
701
|
-
print(' 1. Generate a Medplum CDK config file (i.e., medplum.demo.config.json)');
|
|
702
|
-
print(' 2. Optionally generate an AWS CloudFront signing key');
|
|
703
|
-
print(' 3. Optionally request SSL certificates from AWS Certificate Manager');
|
|
704
|
-
print(' 4. Optionally write server config settings to AWS Parameter Store');
|
|
705
|
-
print('');
|
|
706
|
-
print('The Medplum infra config file is an input to the Medplum CDK.');
|
|
707
|
-
print('The Medplum CDK will create and manage the necessary AWS resources.');
|
|
708
|
-
print('');
|
|
709
|
-
print('We will ask a series of questions to generate your infra config file.');
|
|
710
|
-
print('Some questions have predefined options in [square brackets].');
|
|
711
|
-
print('Some questions have default values in (parentheses), which you can accept by pressing Enter.');
|
|
712
|
-
print('Press Ctrl+C at any time to exit.');
|
|
713
|
-
header('ENVIRONMENT NAME');
|
|
714
|
-
print('Medplum deployments have a short environment name such as "prod", "staging", "alice", or "demo".');
|
|
715
|
-
print('The environment name is used in multiple places:');
|
|
716
|
-
print(' 1. As part of config file names (i.e., medplum.demo.config.json)');
|
|
717
|
-
print(' 2. As the base of CloudFormation stack names (i.e., MedplumDemo)');
|
|
718
|
-
print(' 3. AWS Parameter Store keys (i.e., /medplum/demo/...)');
|
|
719
|
-
config.name = await ask('What is your environment name?', 'demo');
|
|
720
|
-
print('Using environment name "' + config.name + '"...');
|
|
721
|
-
header('CONFIG FILE');
|
|
722
|
-
print('Medplum Infrastructure will create a config file in the current directory.');
|
|
723
|
-
const configFileName = await ask('What is the config file name?', `medplum.${config.name}.config.json`);
|
|
724
|
-
if (fs.existsSync(configFileName)) {
|
|
725
|
-
print('Config file already exists.');
|
|
726
|
-
await checkOk('Do you want to overwrite the config file?');
|
|
727
|
-
}
|
|
728
|
-
print('Using config file "' + configFileName + '"...');
|
|
729
|
-
writeConfig(configFileName, config);
|
|
730
|
-
header('AWS REGION');
|
|
731
|
-
print('Most Medplum resources will be created in a single AWS region.');
|
|
732
|
-
config.region = await ask('Enter your AWS region:', 'us-east-1');
|
|
733
|
-
writeConfig(configFileName, config);
|
|
734
|
-
header('AWS ACCOUNT NUMBER');
|
|
735
|
-
print('Medplum Infrastructure will use your AWS account number to create AWS resources.');
|
|
736
|
-
const currentAccountId = await getAccountId(config.region);
|
|
737
|
-
print('Using the AWS CLI, your current account ID is: ' + currentAccountId);
|
|
738
|
-
config.accountNumber = await ask('What is your AWS account number?', currentAccountId);
|
|
739
|
-
writeConfig(configFileName, config);
|
|
740
|
-
header('STACK NAME');
|
|
741
|
-
print('Medplum will create a CloudFormation stack to manage AWS resources.');
|
|
742
|
-
const defaultStackName = 'Medplum' + config.name.charAt(0).toUpperCase() + config.name.slice(1);
|
|
743
|
-
config.stackName = await ask('Enter your CloudFormation stack name?', defaultStackName);
|
|
744
|
-
writeConfig(configFileName, config);
|
|
745
|
-
header('BASE DOMAIN NAME');
|
|
746
|
-
print('Medplum deploys multiple subdomains for various services.');
|
|
747
|
-
print('');
|
|
748
|
-
print('For example, "api." for the REST API and "app." for the web application.');
|
|
749
|
-
print('The base domain name is the common suffix for all subdomains.');
|
|
750
|
-
print('');
|
|
751
|
-
print('For example, if your base domain name is "example.com",');
|
|
752
|
-
print('then the REST API will be "api.example.com".');
|
|
753
|
-
print('');
|
|
754
|
-
print('Note that you must own the base domain, and it must use Route53 DNS.');
|
|
755
|
-
print('Medplum will create subdomains for you, but you must configure the base domain.');
|
|
756
|
-
while (!config.domainName) {
|
|
757
|
-
config.domainName = await ask('Enter your base domain name:');
|
|
758
|
-
}
|
|
759
|
-
writeConfig(configFileName, config);
|
|
760
|
-
header('SUPPORT EMAIL');
|
|
761
|
-
print('Medplum sends transactional emails to users.');
|
|
762
|
-
print('For example, emails to new users or for password reset.');
|
|
763
|
-
print('Medplum will use the support email address to send these emails.');
|
|
764
|
-
print('Note that you must verify the support email address in SES.');
|
|
765
|
-
const supportEmail = await ask('Enter your support email address:');
|
|
766
|
-
header('API DOMAIN NAME');
|
|
767
|
-
print('Medplum deploys a REST API for the backend services.');
|
|
768
|
-
config.apiDomainName = await ask('Enter your REST API domain name:', 'api.' + config.domainName);
|
|
769
|
-
writeConfig(configFileName, config);
|
|
770
|
-
header('APP DOMAIN NAME');
|
|
771
|
-
print('Medplum deploys a web application for the user interface.');
|
|
772
|
-
config.appDomainName = await ask('Enter your web application domain name:', 'app.' + config.domainName);
|
|
773
|
-
writeConfig(configFileName, config);
|
|
774
|
-
header('STORAGE DOMAIN NAME');
|
|
775
|
-
print('Medplum deploys a storage service for file uploads.');
|
|
776
|
-
config.storageDomainName = await ask('Enter your storage domain name:', 'storage.' + config.domainName);
|
|
777
|
-
writeConfig(configFileName, config);
|
|
778
|
-
header('STORAGE BUCKET');
|
|
779
|
-
print('Medplum uses an S3 bucket to store binary content such as file uploads.');
|
|
780
|
-
print('Medplum will create a the S3 bucket as part of the CloudFormation stack.');
|
|
781
|
-
config.storageBucketName = await ask('Enter your storage bucket name:', 'medplum-' + config.name + '-storage');
|
|
782
|
-
writeConfig(configFileName, config);
|
|
783
|
-
header('MAX AVAILABILITY ZONES');
|
|
784
|
-
print('Medplum API servers can be deployed in multiple availability zones.');
|
|
785
|
-
print('This provides redundancy and high availability.');
|
|
786
|
-
print('However, it also increases the cost of the deployment.');
|
|
787
|
-
print('If you want to use all availability zones, choose a large number such as 99.');
|
|
788
|
-
print('If you want to restrict the number, for example to manage EIP limits,');
|
|
789
|
-
print('then choose a small number such as 1 or 2.');
|
|
790
|
-
config.maxAzs = await chooseInt('Enter the maximum number of availability zones:', [1, 2, 3, 99], 2);
|
|
791
|
-
header('DATABASE INSTANCES');
|
|
792
|
-
print('Medplum uses a relational database to store data.');
|
|
793
|
-
print('You can set up your own database,');
|
|
794
|
-
print('or Medplum can create a new RDS database as part of the CloudFormation stack.');
|
|
795
|
-
if (await yesOrNo('Do you want to create a new RDS database as part of the CloudFormation stack?')) {
|
|
796
|
-
print('Medplum will create a new RDS database as part of the CloudFormation stack.');
|
|
797
|
-
print('');
|
|
798
|
-
print('If you need high availability, you can choose multiple instances.');
|
|
799
|
-
print('Use 1 for a single instance, or 2 for a primary and a standby.');
|
|
800
|
-
config.rdsInstances = await chooseInt('Enter the number of database instances:', [1, 2], 1);
|
|
801
|
-
}
|
|
802
|
-
else {
|
|
803
|
-
print('Medplum will not create a new RDS database.');
|
|
804
|
-
print('Please create a new RDS database and enter the database name, username, and password.');
|
|
805
|
-
print('Set the AWS Secrets Manager secret ARN in the config file in the "rdsSecretsArn" setting.');
|
|
806
|
-
config.rdsSecretsArn = 'TODO';
|
|
807
|
-
}
|
|
808
|
-
writeConfig(configFileName, config);
|
|
809
|
-
header('SERVER INSTANCES');
|
|
810
|
-
print('Medplum uses AWS Fargate to run the API servers.');
|
|
811
|
-
print('Medplum will create a new Fargate cluster as part of the CloudFormation stack.');
|
|
812
|
-
print('Fargate will automatically scale the number of servers up and down.');
|
|
813
|
-
print('If you need high availability, you can choose multiple instances.');
|
|
814
|
-
config.desiredServerCount = await chooseInt('Enter the number of server instances:', [1, 2, 3, 4, 6, 8], 1);
|
|
815
|
-
writeConfig(configFileName, config);
|
|
816
|
-
header('SERVER MEMORY');
|
|
817
|
-
print('You can choose the amount of memory for each server instance.');
|
|
818
|
-
print('The default is 512 MB, which is sufficient for getting started.');
|
|
819
|
-
print('Note that only certain CPU units are compatible with memory units.');
|
|
820
|
-
print('Consult AWS Fargate "Task Definition Parameters" for more information.');
|
|
821
|
-
config.serverMemory = await chooseInt('Enter the server memory (MB):', [512, 1024, 2048, 4096, 8192, 16384], 512);
|
|
822
|
-
writeConfig(configFileName, config);
|
|
823
|
-
header('SERVER CPU');
|
|
824
|
-
print('You can choose the amount of CPU for each server instance.');
|
|
825
|
-
print('CPU is expressed as an integer using AWS CPU units');
|
|
826
|
-
print('The default is 256, which is sufficient for getting started.');
|
|
827
|
-
print('Note that only certain CPU units are compatible with memory units.');
|
|
828
|
-
print('Consult AWS Fargate "Task Definition Parameters" for more information.');
|
|
829
|
-
config.serverCpu = await chooseInt('Enter the server CPU:', [256, 512, 1024, 2048, 4096, 8192, 16384], 256);
|
|
830
|
-
writeConfig(configFileName, config);
|
|
831
|
-
header('SERVER IMAGE');
|
|
832
|
-
print('Medplum uses Docker images for the API servers.');
|
|
833
|
-
print('You can choose the image to use for the servers.');
|
|
834
|
-
print('Docker images can be loaded from either Docker Hub or AWS ECR.');
|
|
835
|
-
print('The default is the latest Medplum release.');
|
|
836
|
-
config.serverImage = await ask('Enter the server image:', 'medplum/medplum-server:latest');
|
|
837
|
-
writeConfig(configFileName, config);
|
|
838
|
-
header('SIGNING KEY');
|
|
839
|
-
print('Medplum uses AWS CloudFront Presigned URLs for binary content such as file uploads.');
|
|
840
|
-
const { privateKey, publicKey, passphrase } = generateSigningKey();
|
|
841
|
-
config.storagePublicKey = publicKey;
|
|
842
|
-
writeConfig(configFileName, config);
|
|
843
|
-
header('SSL CERTIFICATES');
|
|
844
|
-
print(`Medplum will now check for existing SSL certificates for the subdomains.`);
|
|
845
|
-
const allCerts = await listAllCertificates(config.region);
|
|
846
|
-
print('Found ' + allCerts.length + ' certificate(s).');
|
|
847
|
-
// Process certificates for each subdomain
|
|
848
|
-
// Note: The "api" certificate must be created in the same region as the API
|
|
849
|
-
// Note: The "app" and "storage" certificates must be created in us-east-1
|
|
850
|
-
for (const { region, certName } of [
|
|
851
|
-
{ region: config.region, certName: 'api' },
|
|
852
|
-
{ region: 'us-east-1', certName: 'app' },
|
|
853
|
-
{ region: 'us-east-1', certName: 'storage' },
|
|
854
|
-
]) {
|
|
855
|
-
print('');
|
|
856
|
-
const arn = await processCert(config, allCerts, region, certName);
|
|
857
|
-
config[getDomainCertSetting(certName)] = arn;
|
|
858
|
-
writeConfig(configFileName, config);
|
|
859
|
-
}
|
|
860
|
-
header('AWS PARAMETER STORE');
|
|
861
|
-
print('Medplum uses AWS Parameter Store to store sensitive configuration values.');
|
|
862
|
-
print('These values will be encrypted at rest.');
|
|
863
|
-
print(`The values will be stored in the "/medplum/${config.name}" path.`);
|
|
864
|
-
const serverParams = {
|
|
865
|
-
port: config.apiPort,
|
|
866
|
-
baseUrl: `https://${config.apiDomainName}/`,
|
|
867
|
-
appBaseUrl: `https://${config.appDomainName}/`,
|
|
868
|
-
storageBaseUrl: `https://${config.storageDomainName}/binary/`,
|
|
869
|
-
binaryStorage: `s3:${config.storageBucketName}`,
|
|
870
|
-
signingKey: privateKey,
|
|
871
|
-
signingKeyPassphrase: passphrase,
|
|
872
|
-
supportEmail: supportEmail,
|
|
873
|
-
};
|
|
874
|
-
print(JSON.stringify({
|
|
875
|
-
...serverParams,
|
|
876
|
-
signingKey: '****',
|
|
877
|
-
signingKeyPassphrase: '****',
|
|
878
|
-
}, null, 2));
|
|
879
|
-
await checkOk('Do you want to store these values in AWS Parameter Store?');
|
|
880
|
-
await writeParameters(config.region, `/medplum/${config.name}/`, serverParams);
|
|
881
|
-
header('DONE!');
|
|
882
|
-
print('Medplum configuration complete.');
|
|
883
|
-
print('You can now proceed to deploying the Medplum infrastructure with CDK.');
|
|
884
|
-
print('Run:');
|
|
885
|
-
print('');
|
|
886
|
-
print(` npx cdk bootstrap -c config=${configFileName}`);
|
|
887
|
-
print(` npx cdk synth -c config=${configFileName}`);
|
|
888
|
-
if (config.region === 'us-east-1') {
|
|
889
|
-
print(` npx cdk deploy -c config=${configFileName}`);
|
|
890
|
-
}
|
|
891
|
-
else {
|
|
892
|
-
print(` npx cdk deploy -c config=${configFileName} --all`);
|
|
893
|
-
}
|
|
894
|
-
print('');
|
|
895
|
-
print('See Medplum documentation for more information:');
|
|
896
|
-
print('');
|
|
897
|
-
print(' https://www.medplum.com/docs/self-hosting/install-on-aws');
|
|
898
|
-
print('');
|
|
899
|
-
}
|
|
900
|
-
/**
|
|
901
|
-
* Prints to stdout.
|
|
902
|
-
* @param text The text to print.
|
|
903
|
-
*/
|
|
904
|
-
function print(text) {
|
|
905
|
-
terminal.write(text + '\n');
|
|
906
|
-
}
|
|
907
|
-
/**
|
|
908
|
-
* Prints a header with extra line spacing.
|
|
909
|
-
* @param text The text to print.
|
|
910
|
-
*/
|
|
911
|
-
function header(text) {
|
|
912
|
-
print('\n' + text + '\n');
|
|
913
|
-
}
|
|
914
|
-
/**
|
|
915
|
-
* Prints a question and waits for user input.
|
|
916
|
-
* @param text The question text to print.
|
|
917
|
-
* @param defaultValue Optional default value.
|
|
918
|
-
* @returns The selected value, or default value on empty selection.
|
|
919
|
-
*/
|
|
920
|
-
function ask(text, defaultValue = '') {
|
|
921
|
-
return new Promise((resolve) => {
|
|
922
|
-
terminal.question(text + (defaultValue ? ' (' + defaultValue + ')' : '') + ' ', (answer) => {
|
|
923
|
-
resolve(answer || defaultValue.toString());
|
|
924
|
-
});
|
|
925
|
-
});
|
|
926
|
-
}
|
|
927
|
-
/**
|
|
928
|
-
* Prints a question and waits for user to choose one of the provided options.
|
|
929
|
-
* @param text The prompt text to print.
|
|
930
|
-
* @param options The list of options that the user can select.
|
|
931
|
-
* @param defaultValue Optional default value.
|
|
932
|
-
* @returns The selected value, or default value on empty selection.
|
|
933
|
-
*/
|
|
934
|
-
async function choose(text, options, defaultValue = '') {
|
|
935
|
-
const str = text + ' [' + options.map((o) => (o === defaultValue ? '(' + o + ')' : o)).join('|') + ']';
|
|
936
|
-
// eslint-disable-next-line no-constant-condition
|
|
937
|
-
while (true) {
|
|
938
|
-
const answer = (await ask(str)) || defaultValue;
|
|
939
|
-
if (options.includes(answer)) {
|
|
940
|
-
return answer;
|
|
941
|
-
}
|
|
942
|
-
print('Please choose one of the following options: ' + options.join(', '));
|
|
943
|
-
}
|
|
944
|
-
}
|
|
945
|
-
/**
|
|
946
|
-
* Prints a question and waits for the user to choose a valid integer option.
|
|
947
|
-
* @param text The prompt text to print.
|
|
948
|
-
* @param options The list of options that the user can select.
|
|
949
|
-
* @param defaultValue Optional default value.
|
|
950
|
-
* @returns The selected value, or default value on empty selection.
|
|
951
|
-
*/
|
|
952
|
-
async function chooseInt(text, options, defaultValue = 0) {
|
|
953
|
-
return parseInt(await choose(text, options.map((o) => o.toString()), defaultValue.toString()), 10);
|
|
954
|
-
}
|
|
955
|
-
/**
|
|
956
|
-
* Prints a question and waits for the user to choose yes or no.
|
|
957
|
-
* @param text The question to print.
|
|
958
|
-
* @returns true on accept or false on reject.
|
|
959
|
-
*/
|
|
960
|
-
async function yesOrNo(text) {
|
|
961
|
-
return (await choose(text, ['y', 'n'])).toLowerCase() === 'y';
|
|
962
|
-
}
|
|
963
|
-
/**
|
|
964
|
-
* Prints a question and waits for the user to confirm yes. Throws error on no, and exits the program.
|
|
965
|
-
* @param text The prompt text to print.
|
|
966
|
-
*/
|
|
967
|
-
async function checkOk(text) {
|
|
968
|
-
if (!(await yesOrNo(text))) {
|
|
969
|
-
print('Exiting...');
|
|
970
|
-
throw new Error('User cancelled');
|
|
971
|
-
}
|
|
972
|
-
}
|
|
973
|
-
/**
|
|
974
|
-
* Writes a config file to disk.
|
|
975
|
-
* @param configFileName The config file name.
|
|
976
|
-
* @param config The config file contents.
|
|
977
|
-
*/
|
|
978
|
-
function writeConfig(configFileName, config) {
|
|
979
|
-
fs.writeFileSync(path.resolve(configFileName), JSON.stringify(config, undefined, 2), 'utf-8');
|
|
980
|
-
}
|
|
981
|
-
/**
|
|
982
|
-
* Returns the current AWS account ID.
|
|
983
|
-
* This is used as the default value for the "accountNumber" config setting.
|
|
984
|
-
* @param region The AWS region.
|
|
985
|
-
* @returns The AWS account ID.
|
|
986
|
-
*/
|
|
987
|
-
async function getAccountId(region) {
|
|
988
|
-
try {
|
|
989
|
-
const client = new clientSts.STSClient({ region });
|
|
990
|
-
const command = new clientSts.GetCallerIdentityCommand({});
|
|
991
|
-
const response = await client.send(command);
|
|
992
|
-
return response.Account;
|
|
993
|
-
}
|
|
994
|
-
catch (err) {
|
|
995
|
-
console.log('Warning: Unable to get AWS account ID', err.message);
|
|
996
|
-
return undefined;
|
|
997
|
-
}
|
|
998
|
-
}
|
|
999
|
-
/**
|
|
1000
|
-
* Returns a list of all AWS certificates.
|
|
1001
|
-
* This is used to find existing certificates for the subdomains.
|
|
1002
|
-
* If the primary region is not us-east-1, then certificates in us-east-1 will also be returned.
|
|
1003
|
-
* @param region The AWS region.
|
|
1004
|
-
* @returns The list of AWS Certificates.
|
|
1005
|
-
*/
|
|
1006
|
-
async function listAllCertificates(region) {
|
|
1007
|
-
const result = await listCertificates(region);
|
|
1008
|
-
if (region !== 'us-east-1') {
|
|
1009
|
-
const usEast1Result = await listCertificates('us-east-1');
|
|
1010
|
-
result.push(...usEast1Result);
|
|
1011
|
-
}
|
|
1012
|
-
return result;
|
|
1013
|
-
}
|
|
1014
|
-
/**
|
|
1015
|
-
* Returns a list of AWS Certificates.
|
|
1016
|
-
* This is used to find existing certificates for the subdomains.
|
|
1017
|
-
* @param region The AWS region.
|
|
1018
|
-
* @returns The list of AWS Certificates.
|
|
1019
|
-
*/
|
|
1020
|
-
async function listCertificates(region) {
|
|
1021
|
-
try {
|
|
1022
|
-
const client = new clientAcm.ACMClient({ region });
|
|
1023
|
-
const command = new clientAcm.ListCertificatesCommand({ MaxItems: 1000 });
|
|
1024
|
-
const response = await client.send(command);
|
|
1025
|
-
return response.CertificateSummaryList;
|
|
1026
|
-
}
|
|
1027
|
-
catch (err) {
|
|
1028
|
-
console.log('Warning: Unable to list certificates', err.message);
|
|
1029
|
-
return [];
|
|
1030
|
-
}
|
|
1031
|
-
}
|
|
1032
|
-
/**
|
|
1033
|
-
* Processes a required certificate.
|
|
1034
|
-
*
|
|
1035
|
-
* 1. If the certificate already exists, return the ARN.
|
|
1036
|
-
* 2. If the certificate does not exist, and the user wants to create a new certificate, create it and return the ARN.
|
|
1037
|
-
* 3. If the certificate does not exist, and the user does not want to create a new certificate, return a placeholder.
|
|
1038
|
-
* @param config In-progress config settings.
|
|
1039
|
-
* @param allCerts List of all existing certificates.
|
|
1040
|
-
* @param region The AWS region where the certificate is needed.
|
|
1041
|
-
* @param certName The name of the certificate (api, app, or storage).
|
|
1042
|
-
* @returns The ARN of the certificate or placeholder if a new certificate is needed.
|
|
1043
|
-
*/
|
|
1044
|
-
async function processCert(config, allCerts, region, certName) {
|
|
1045
|
-
const domainName = config[getDomainSetting(certName)];
|
|
1046
|
-
const existingCert = allCerts.find((cert) => cert.CertificateArn?.includes(region) && cert.DomainName === domainName);
|
|
1047
|
-
if (existingCert) {
|
|
1048
|
-
print(`Found existing certificate for "${domainName}" in "${region}.`);
|
|
1049
|
-
return existingCert.CertificateArn;
|
|
1050
|
-
}
|
|
1051
|
-
print(`No existing certificate found for "${domainName}" in "${region}.`);
|
|
1052
|
-
if (!(await yesOrNo('Do you want to request a new certificate?'))) {
|
|
1053
|
-
print(`Please add your certificate ARN to the config file in the "${getDomainCertSetting(certName)}" setting.`);
|
|
1054
|
-
return 'TODO';
|
|
1055
|
-
}
|
|
1056
|
-
const arn = await requestCert(region, domainName);
|
|
1057
|
-
print('Certificate ARN: ' + arn);
|
|
1058
|
-
return arn;
|
|
1059
|
-
}
|
|
1060
|
-
/**
|
|
1061
|
-
* Requests an AWS Certificate.
|
|
1062
|
-
* @param region The AWS region.
|
|
1063
|
-
* @param domain The domain name.
|
|
1064
|
-
* @returns The AWS Certificate ARN on success, or undefined on failure.
|
|
1065
|
-
*/
|
|
1066
|
-
async function requestCert(region, domain) {
|
|
1067
|
-
try {
|
|
1068
|
-
const validationMethod = await choose('Validate certificate using DNS or email validation?', ['dns', 'email'], 'dns');
|
|
1069
|
-
const client = new clientAcm.ACMClient({ region });
|
|
1070
|
-
const command = new clientAcm.RequestCertificateCommand({
|
|
1071
|
-
DomainName: domain,
|
|
1072
|
-
ValidationMethod: validationMethod.toUpperCase(),
|
|
1073
|
-
});
|
|
1074
|
-
const response = await client.send(command);
|
|
1075
|
-
return response.CertificateArn;
|
|
1076
|
-
}
|
|
1077
|
-
catch (err) {
|
|
1078
|
-
console.log('Error: Unable to request certificate', err.message);
|
|
1079
|
-
return 'TODO';
|
|
1080
|
-
}
|
|
1081
|
-
}
|
|
1082
|
-
/**
|
|
1083
|
-
* Generates an AWS CloudFront signing key.
|
|
1084
|
-
*
|
|
1085
|
-
* Requirements:
|
|
1086
|
-
*
|
|
1087
|
-
* 1. It must be an SSH-2 RSA key pair.
|
|
1088
|
-
* 2. It must be in base64-encoded PEM format.
|
|
1089
|
-
* 3. It must be a 2048-bit key pair.
|
|
1090
|
-
*
|
|
1091
|
-
* See: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-trusted-signers.html#private-content-creating-cloudfront-key-pairs
|
|
1092
|
-
* @returns A new signing key.
|
|
1093
|
-
*/
|
|
1094
|
-
function generateSigningKey() {
|
|
1095
|
-
const passphrase = crypto.randomUUID();
|
|
1096
|
-
const signingKey = crypto.generateKeyPairSync('rsa', {
|
|
1097
|
-
modulusLength: 2048,
|
|
1098
|
-
publicKeyEncoding: {
|
|
1099
|
-
type: 'spki',
|
|
1100
|
-
format: 'pem',
|
|
1101
|
-
},
|
|
1102
|
-
privateKeyEncoding: {
|
|
1103
|
-
type: 'pkcs1',
|
|
1104
|
-
format: 'pem',
|
|
1105
|
-
cipher: 'aes-256-cbc',
|
|
1106
|
-
passphrase,
|
|
1107
|
-
},
|
|
1108
|
-
});
|
|
1109
|
-
return {
|
|
1110
|
-
publicKey: signingKey.publicKey,
|
|
1111
|
-
privateKey: signingKey.privateKey,
|
|
1112
|
-
passphrase,
|
|
1113
|
-
};
|
|
1114
|
-
}
|
|
1115
|
-
/**
|
|
1116
|
-
* Writes a parameter to AWS Parameter Store.
|
|
1117
|
-
* @param region The AWS region.
|
|
1118
|
-
* @param key The parameter key.
|
|
1119
|
-
* @param value The parameter value.
|
|
1120
|
-
*/
|
|
1121
|
-
async function writeParameter(region, key, value) {
|
|
1122
|
-
const client = new clientSsm.SSMClient({ region });
|
|
1123
|
-
const command = new clientSsm.PutParameterCommand({
|
|
1124
|
-
Name: key,
|
|
1125
|
-
Value: value,
|
|
1126
|
-
Type: 'SecureString',
|
|
1127
|
-
Overwrite: true,
|
|
1128
|
-
});
|
|
1129
|
-
await client.send(command);
|
|
1130
|
-
}
|
|
1131
|
-
/**
|
|
1132
|
-
* Writes a collection of parameters to AWS Parameter Store.
|
|
1133
|
-
* @param region The AWS region.
|
|
1134
|
-
* @param prefix The AWS Parameter Store prefix.
|
|
1135
|
-
* @param params The parameters to write.
|
|
1136
|
-
*/
|
|
1137
|
-
async function writeParameters(region, prefix, params) {
|
|
1138
|
-
for (const [key, value] of Object.entries(params)) {
|
|
1139
|
-
const valueStr = value.toString();
|
|
1140
|
-
if (valueStr) {
|
|
1141
|
-
await writeParameter(region, prefix + key, valueStr);
|
|
1142
|
-
}
|
|
1143
|
-
}
|
|
1144
|
-
}
|
|
1145
|
-
|
|
1146
|
-
const aws = new commander.Command('aws').description('Commands to manage AWS resources');
|
|
1147
|
-
aws.command('init').description('Initialize a new Medplum AWS CloudFormation stacks').action(initStackCommand);
|
|
1148
|
-
aws.command('list').description('List Medplum AWS CloudFormation stacks').action(listStacksCommand);
|
|
1149
|
-
aws
|
|
1150
|
-
.command('describe')
|
|
1151
|
-
.description('Describe a Medplum AWS CloudFormation stack by tag')
|
|
1152
|
-
.argument('<tag>')
|
|
1153
|
-
.action(describeStacksCommand);
|
|
1154
|
-
aws.command('update-server').description('Update the server image').argument('<tag>').action(updateServerCommand);
|
|
1155
|
-
aws.command('update-app').description('Update the app site').argument('<tag>').action(updateAppCommand);
|
|
1156
|
-
|
|
1157
|
-
const botSaveCommand = createMedplumCommand('save');
|
|
1158
|
-
const botDeployCommand = createMedplumCommand('deploy');
|
|
1159
|
-
const botCreateCommand = createMedplumCommand('create');
|
|
1160
|
-
const bot = new commander.Command('bot')
|
|
1161
|
-
.addCommand(botSaveCommand)
|
|
1162
|
-
.addCommand(botDeployCommand)
|
|
1163
|
-
.addCommand(botCreateCommand);
|
|
1164
|
-
// Commands to deprecate
|
|
1165
|
-
const saveBotDeprecate = createMedplumCommand('save-bot');
|
|
1166
|
-
const deployBotDeprecate = createMedplumCommand('deploy-bot');
|
|
1167
|
-
const createBotDeprecate = createMedplumCommand('create-bot');
|
|
1168
|
-
botSaveCommand
|
|
1169
|
-
.description('Saving the bot')
|
|
1170
|
-
.argument('<botName>')
|
|
1171
|
-
.action(async (botName, options) => {
|
|
1172
|
-
const medplum = await createMedplumClient(options);
|
|
1173
|
-
await botWrapper(medplum, botName);
|
|
1174
|
-
});
|
|
1175
|
-
botDeployCommand
|
|
1176
|
-
.description('Deploy the app to AWS')
|
|
1177
|
-
.argument('<botName>')
|
|
1178
|
-
.action(async (botName, options) => {
|
|
1179
|
-
const medplum = await createMedplumClient(options);
|
|
1180
|
-
await botWrapper(medplum, botName, true);
|
|
1181
|
-
});
|
|
1182
|
-
botCreateCommand
|
|
1183
|
-
.arguments('<botName> <projectId> <sourceFile> <distFile>')
|
|
1184
|
-
.description('Creating a bot')
|
|
1185
|
-
.action(async (botName, projectId, sourceFile, distFile, options) => {
|
|
1186
|
-
const medplum = await createMedplumClient(options);
|
|
1187
|
-
await createBot(medplum, [botName, projectId, sourceFile, distFile]);
|
|
1188
|
-
});
|
|
1189
|
-
async function botWrapper(medplum, botName, deploy = false) {
|
|
1190
|
-
const botConfigs = readBotConfigs(botName);
|
|
1191
|
-
for (const botConfig of botConfigs) {
|
|
1192
|
-
const bot = await medplum.readResource('Bot', botConfig.id);
|
|
1193
|
-
await saveBot(medplum, botConfig, bot);
|
|
1194
|
-
if (deploy) {
|
|
1195
|
-
await deployBot(medplum, botConfig, bot);
|
|
1196
|
-
}
|
|
1197
|
-
}
|
|
1198
|
-
console.log(`Number of bots deployed: ${botConfigs.length}`);
|
|
1199
|
-
}
|
|
1200
|
-
// Deprecate bot commands
|
|
1201
|
-
saveBotDeprecate
|
|
1202
|
-
.description('Saves the bot')
|
|
1203
|
-
.argument('<botName>')
|
|
1204
|
-
.action(async (botName, options) => {
|
|
1205
|
-
const medplum = await createMedplumClient(options);
|
|
1206
|
-
await botWrapper(medplum, botName);
|
|
1207
|
-
});
|
|
1208
|
-
deployBotDeprecate
|
|
1209
|
-
.description('Deploy the bot to AWS')
|
|
1210
|
-
.argument('<botName>')
|
|
1211
|
-
.action(async (botName, options) => {
|
|
1212
|
-
const medplum = await createMedplumClient(options);
|
|
1213
|
-
await botWrapper(medplum, botName, true);
|
|
1214
|
-
});
|
|
1215
|
-
createBotDeprecate
|
|
1216
|
-
.arguments('<botName> <projectId> <sourceFile> <distFile>')
|
|
1217
|
-
.description('Creates and saves the bot')
|
|
1218
|
-
.action(async (botName, projectId, sourceFile, distFile, options) => {
|
|
1219
|
-
const medplum = await createMedplumClient(options);
|
|
1220
|
-
await createBot(medplum, [botName, projectId, sourceFile, distFile]);
|
|
1221
|
-
});
|
|
1222
|
-
|
|
1223
|
-
const bulkExportCommand = createMedplumCommand('export');
|
|
1224
|
-
const bulkImportCommand = createMedplumCommand('import');
|
|
1225
|
-
const bulk = new commander.Command('bulk').addCommand(bulkExportCommand).addCommand(bulkImportCommand);
|
|
1226
|
-
bulkExportCommand
|
|
1227
|
-
.option('-e, --export-level <exportLevel>', 'Optional export level. Defaults to system level export. "Group/:id" - Group of Patients, "Patient" - All Patients.')
|
|
1228
|
-
.option('-t, --types <types>', 'optional resource types to export')
|
|
1229
|
-
.option('-s, --since <since>', 'optional Resources will be included in the response if their state has changed after the supplied time (e.g. if Resource.meta.lastUpdated is later than the supplied _since time).')
|
|
1230
|
-
.option('-d, --target-directory <targetDirectory>', 'optional target directory to save files from the bulk export operations.')
|
|
1231
|
-
.action(async (options) => {
|
|
1232
|
-
const { exportLevel, types, since, targetDirectory } = options;
|
|
1233
|
-
const medplum = await createMedplumClient(options);
|
|
1234
|
-
const response = await medplum.bulkExport(exportLevel, types, since);
|
|
1235
|
-
response.output?.forEach(async ({ type, url }) => {
|
|
1236
|
-
const fileUrl = new URL(url);
|
|
1237
|
-
const data = await medplum.download(url);
|
|
1238
|
-
const fileName = `${type}_${fileUrl.pathname}`.replace(/[^a-zA-Z0-9]+/g, '_') + '.ndjson';
|
|
1239
|
-
const path$1 = path.resolve(targetDirectory ?? '', fileName);
|
|
1240
|
-
fs.writeFile(`${path$1}`, await data.text(), () => {
|
|
1241
|
-
console.log(`${path$1} is created`);
|
|
1242
|
-
});
|
|
1243
|
-
});
|
|
1244
|
-
});
|
|
1245
|
-
bulkImportCommand
|
|
1246
|
-
.argument('<filename>', 'File Name')
|
|
1247
|
-
.option('--num-resources-per-request <numResourcesPerRequest>', 'optional number of resources to import per batch request. Defaults to 25.', '25')
|
|
1248
|
-
.option('--add-extensions-for-missing-values', 'optional flag to add extensions for missing values in a resource', false)
|
|
1249
|
-
.option('-d, --target-directory <targetDirectory>', 'optional target directory of file to be imported')
|
|
1250
|
-
.action(async (fileName, options) => {
|
|
1251
|
-
const { numResourcesPerRequest, addExtensionsForMissingValues, targetDirectory } = options;
|
|
1252
|
-
const path$1 = path.resolve(targetDirectory ?? process.cwd(), fileName);
|
|
1253
|
-
const medplum = await createMedplumClient(options);
|
|
1254
|
-
await importFile(path$1, parseInt(numResourcesPerRequest, 10), medplum, addExtensionsForMissingValues);
|
|
1255
|
-
});
|
|
1256
|
-
async function importFile(path, numResourcesPerRequest, medplum, addExtensionsForMissingValues) {
|
|
1257
|
-
let entries = [];
|
|
1258
|
-
const fileStream = fs.createReadStream(path);
|
|
1259
|
-
const rl = readline.createInterface({
|
|
1260
|
-
input: fileStream,
|
|
1261
|
-
});
|
|
1262
|
-
for await (const line of rl) {
|
|
1263
|
-
const resource = parseResource(line, addExtensionsForMissingValues);
|
|
1264
|
-
entries.push({
|
|
1265
|
-
resource: resource,
|
|
1266
|
-
request: {
|
|
1267
|
-
method: 'POST',
|
|
1268
|
-
url: resource.resourceType,
|
|
1269
|
-
},
|
|
1270
|
-
});
|
|
1271
|
-
if (entries.length % numResourcesPerRequest === 0) {
|
|
1272
|
-
await sendBatchEntries(entries, medplum);
|
|
1273
|
-
entries = [];
|
|
1274
|
-
}
|
|
1275
|
-
}
|
|
1276
|
-
if (entries.length > 0) {
|
|
1277
|
-
await sendBatchEntries(entries, medplum);
|
|
1278
|
-
}
|
|
1279
|
-
}
|
|
1280
|
-
async function sendBatchEntries(entries, medplum) {
|
|
1281
|
-
const result = await medplum.executeBatch({
|
|
1282
|
-
resourceType: 'Bundle',
|
|
1283
|
-
type: 'transaction',
|
|
1284
|
-
entry: entries,
|
|
1285
|
-
});
|
|
1286
|
-
result.entry?.forEach((resultEntry) => {
|
|
1287
|
-
prettyPrint(resultEntry.response);
|
|
1288
|
-
});
|
|
1289
|
-
}
|
|
1290
|
-
function parseResource(jsonString, addExtensionsForMissingValues) {
|
|
1291
|
-
const resource = JSON.parse(jsonString);
|
|
1292
|
-
if (addExtensionsForMissingValues) {
|
|
1293
|
-
return addExtensionsForMissingValuesResource(resource);
|
|
1294
|
-
}
|
|
1295
|
-
return resource;
|
|
1296
|
-
}
|
|
1297
|
-
function addExtensionsForMissingValuesResource(resource) {
|
|
1298
|
-
if (resource.resourceType === 'ExplanationOfBenefit') {
|
|
1299
|
-
return addExtensionsForMissingValuesExplanationOfBenefits(resource);
|
|
1300
|
-
}
|
|
1301
|
-
return resource;
|
|
1302
|
-
}
|
|
1303
|
-
function addExtensionsForMissingValuesExplanationOfBenefits(resource) {
|
|
1304
|
-
if (!resource.provider) {
|
|
1305
|
-
resource.provider = getUnsupportedExtension();
|
|
1306
|
-
}
|
|
1307
|
-
resource.item?.forEach((item) => {
|
|
1308
|
-
if (!item?.productOrService) {
|
|
1309
|
-
item.productOrService = getUnsupportedExtension();
|
|
1310
|
-
}
|
|
1311
|
-
});
|
|
1312
|
-
return resource;
|
|
1313
|
-
}
|
|
1314
|
-
|
|
1315
|
-
const projectListCommand = createMedplumCommand('list');
|
|
1316
|
-
const projectCurrentCommand = createMedplumCommand('current');
|
|
1317
|
-
const projectSwitchCommand = createMedplumCommand('switch');
|
|
1318
|
-
const projectInviteCommand = createMedplumCommand('invite');
|
|
1319
|
-
const project = new commander.Command('project')
|
|
1320
|
-
.addCommand(projectListCommand)
|
|
1321
|
-
.addCommand(projectCurrentCommand)
|
|
1322
|
-
.addCommand(projectSwitchCommand)
|
|
1323
|
-
.addCommand(projectInviteCommand);
|
|
1324
|
-
projectListCommand.description('List of current projects').action(async (options) => {
|
|
1325
|
-
const medplum = await createMedplumClient(options);
|
|
1326
|
-
projectList(medplum);
|
|
1327
|
-
});
|
|
1328
|
-
function projectList(medplum) {
|
|
1329
|
-
const logins = medplum.getLogins();
|
|
1330
|
-
const projects = logins
|
|
1331
|
-
.map((login) => `${login.project.display} (${login.project.reference})`)
|
|
1332
|
-
.join('\n\n');
|
|
1333
|
-
console.log(projects);
|
|
1334
|
-
}
|
|
1335
|
-
projectCurrentCommand.description('Project you are currently on').action(async (options) => {
|
|
1336
|
-
const medplum = await createMedplumClient(options);
|
|
1337
|
-
const login = medplum.getActiveLogin();
|
|
1338
|
-
if (!login) {
|
|
1339
|
-
throw new Error('Unauthenticated: run `npx medplum login` to login');
|
|
1340
|
-
}
|
|
1341
|
-
console.log(`${login.project.display} (${login.project.reference})`);
|
|
1342
|
-
});
|
|
1343
|
-
projectSwitchCommand
|
|
1344
|
-
.description('Switching to another project from the current one')
|
|
1345
|
-
.argument('<projectId>')
|
|
1346
|
-
.action(async (projectId, options) => {
|
|
1347
|
-
const medplum = await createMedplumClient(options);
|
|
1348
|
-
await switchProject(medplum, projectId);
|
|
1349
|
-
});
|
|
1350
|
-
projectInviteCommand
|
|
1351
|
-
.description('Invite a member to your current project (run npx medplum project current to confirm)')
|
|
1352
|
-
.arguments('<firstName> <lastName> <email>')
|
|
1353
|
-
.option('--send-email', 'If you want to send the email when inviting the user')
|
|
1354
|
-
.option('--admin', 'If the user you are inviting is an admin')
|
|
1355
|
-
.addOption(new commander.Option('-r, --role <role>', 'Role of user')
|
|
1356
|
-
.choices(['Practitioner', 'Patient', 'RelatedPerson'])
|
|
1357
|
-
.default('Practitioner'))
|
|
1358
|
-
.action(async (firstName, lastName, email, options) => {
|
|
1359
|
-
const medplum = await createMedplumClient(options);
|
|
1360
|
-
const login = medplum.getActiveLogin();
|
|
1361
|
-
if (!login) {
|
|
1362
|
-
throw new Error('Unauthenticated: run `npx medplum login` to login');
|
|
1363
|
-
}
|
|
1364
|
-
if (!login.project.reference) {
|
|
1365
|
-
throw new Error('No current project to invite user to');
|
|
1366
|
-
}
|
|
1367
|
-
const projectId = login.project.reference.split('/')[1];
|
|
1368
|
-
const inviteBody = {
|
|
1369
|
-
resourceType: options.role,
|
|
1370
|
-
firstName,
|
|
1371
|
-
lastName,
|
|
1372
|
-
email,
|
|
1373
|
-
sendEmail: !!options.sendEmail,
|
|
1374
|
-
admin: !!options.admin,
|
|
1375
|
-
};
|
|
1376
|
-
await inviteUser(projectId, inviteBody, medplum);
|
|
1377
|
-
});
|
|
1378
|
-
async function switchProject(medplum, projectId) {
|
|
1379
|
-
const logins = medplum.getLogins();
|
|
1380
|
-
const login = logins.find((login) => login.project.reference?.includes(projectId));
|
|
1381
|
-
if (!login) {
|
|
1382
|
-
console.log(`Error: project ${projectId} not found. Make sure you are added as a user to this project`);
|
|
1383
|
-
}
|
|
1384
|
-
else {
|
|
1385
|
-
await medplum.setActiveLogin(login);
|
|
1386
|
-
console.log(`Switched to project ${projectId}\n`);
|
|
1387
|
-
}
|
|
1388
|
-
}
|
|
1389
|
-
async function inviteUser(projectId, inviteBody, medplum) {
|
|
1390
|
-
try {
|
|
1391
|
-
await medplum.invite(projectId, inviteBody);
|
|
1392
|
-
if (inviteBody.sendEmail) {
|
|
1393
|
-
console.log('Email sent');
|
|
1394
|
-
}
|
|
1395
|
-
console.log('See your users at https://app.medplum.com/admin/users');
|
|
1396
|
-
}
|
|
1397
|
-
catch (err) {
|
|
1398
|
-
console.log('Error while sending invite ' + err);
|
|
1399
|
-
}
|
|
1400
|
-
}
|
|
1401
|
-
|
|
1402
|
-
const deleteObject = createMedplumCommand('delete');
|
|
1403
|
-
const get = createMedplumCommand('get');
|
|
1404
|
-
const patch = createMedplumCommand('patch');
|
|
1405
|
-
const post = createMedplumCommand('post');
|
|
1406
|
-
const put = createMedplumCommand('put');
|
|
1407
|
-
deleteObject.argument('<url>', 'Resource/$id').action(async (url, options) => {
|
|
1408
|
-
const medplum = await createMedplumClient(options);
|
|
1409
|
-
prettyPrint(await medplum.delete(cleanUrl(url)));
|
|
1410
|
-
});
|
|
1411
|
-
get
|
|
1412
|
-
.argument('<url>', 'Resource/$id')
|
|
1413
|
-
.option('--as-transaction', 'Print out the bundle as a transaction type')
|
|
1414
|
-
.action(async (url, options) => {
|
|
1415
|
-
const medplum = await createMedplumClient(options);
|
|
1416
|
-
const response = await medplum.get(cleanUrl(url));
|
|
1417
|
-
if (options.asTransaction) {
|
|
1418
|
-
prettyPrint(core.convertToTransactionBundle(response));
|
|
1419
|
-
}
|
|
1420
|
-
else {
|
|
1421
|
-
prettyPrint(response);
|
|
1422
|
-
}
|
|
1423
|
-
});
|
|
1424
|
-
patch.arguments('<url> <body>').action(async (url, body, options) => {
|
|
1425
|
-
const medplum = await createMedplumClient(options);
|
|
1426
|
-
prettyPrint(await medplum.patch(cleanUrl(url), parseBody(body)));
|
|
1427
|
-
});
|
|
1428
|
-
post.arguments('<url> <body>').action(async (url, body, options) => {
|
|
1429
|
-
const medplum = await createMedplumClient(options);
|
|
1430
|
-
prettyPrint(await medplum.post(cleanUrl(url), parseBody(body)));
|
|
1431
|
-
});
|
|
1432
|
-
put.arguments('<url> <body>').action(async (url, body, options) => {
|
|
1433
|
-
const medplum = await createMedplumClient(options);
|
|
1434
|
-
prettyPrint(await medplum.put(cleanUrl(url), parseBody(body)));
|
|
1435
|
-
});
|
|
1436
|
-
function parseBody(input) {
|
|
1437
|
-
if (!input) {
|
|
1438
|
-
return undefined;
|
|
1439
|
-
}
|
|
1440
|
-
try {
|
|
1441
|
-
return JSON.parse(input);
|
|
1442
|
-
}
|
|
1443
|
-
catch (err) {
|
|
1444
|
-
return input;
|
|
1445
|
-
}
|
|
1446
|
-
}
|
|
1447
|
-
function cleanUrl(input) {
|
|
1448
|
-
const knownPrefixes = ['admin/', 'auth/', 'fhir/R4'];
|
|
1449
|
-
if (knownPrefixes.some((p) => input.startsWith(p))) {
|
|
1450
|
-
// If the URL starts with a known prefix, return it as-is
|
|
1451
|
-
return input;
|
|
1452
|
-
}
|
|
1453
|
-
// Otherwise, default to FHIR
|
|
1454
|
-
return 'fhir/R4/' + input;
|
|
1455
|
-
}
|
|
1456
|
-
|
|
1457
|
-
async function main(argv) {
|
|
1458
|
-
try {
|
|
1459
|
-
const index = new commander.Command('medplum').description('Command to access Medplum CLI');
|
|
1460
|
-
index.version(core.MEDPLUM_VERSION);
|
|
1461
|
-
// Auth commands
|
|
1462
|
-
index.addCommand(login);
|
|
1463
|
-
index.addCommand(whoami);
|
|
1464
|
-
// REST commands
|
|
1465
|
-
index.addCommand(get);
|
|
1466
|
-
index.addCommand(post);
|
|
1467
|
-
index.addCommand(patch);
|
|
1468
|
-
index.addCommand(put);
|
|
1469
|
-
index.addCommand(deleteObject);
|
|
1470
|
-
// Project
|
|
1471
|
-
index.addCommand(project);
|
|
1472
|
-
// Bulk Commands
|
|
1473
|
-
index.addCommand(bulk);
|
|
1474
|
-
// Bot Commands
|
|
1475
|
-
index.addCommand(bot);
|
|
1476
|
-
// Deprecated Bot Commands
|
|
1477
|
-
index.addCommand(saveBotDeprecate);
|
|
1478
|
-
index.addCommand(deployBotDeprecate);
|
|
1479
|
-
index.addCommand(createBotDeprecate);
|
|
1480
|
-
// AWS commands
|
|
1481
|
-
index.addCommand(aws);
|
|
1482
|
-
await index.parseAsync(argv);
|
|
1483
|
-
}
|
|
1484
|
-
catch (err) {
|
|
1485
|
-
console.error('Error: ' + core.normalizeErrorString(err));
|
|
1486
|
-
}
|
|
1487
|
-
}
|
|
1488
|
-
async function run() {
|
|
1489
|
-
dotenv.config();
|
|
1490
|
-
await main(process.argv);
|
|
1491
|
-
}
|
|
1492
|
-
if (require.main === module) {
|
|
1493
|
-
run().catch((err) => console.error('Unhandled error:', err));
|
|
1494
|
-
}
|
|
1495
|
-
|
|
1496
|
-
exports.main = main;
|
|
1497
|
-
exports.run = run;
|
|
7
|
+
`);console.log(o)}ut.description("Project you are currently on").action(async e=>{let o=(await l(e)).getActiveLogin();if(!o)throw new Error("Unauthenticated: run `npx medplum login` to login");console.log(`${o.project.display} (${o.project.reference})`)});pt.description("Switching to another project from the current one").argument("<projectId>").action(async(e,t)=>{let o=await l(t);await no(o,e)});ft.description("Invite a member to your current project (run npx medplum project current to confirm)").arguments("<firstName> <lastName> <email>").option("--send-email","If you want to send the email when inviting the user").option("--admin","If the user you are inviting is an admin").addOption(new K.Option("-r, --role <role>","Role of user").choices(["Practitioner","Patient","RelatedPerson"]).default("Practitioner")).action(async(e,t,o,n)=>{let a=await l(n),i=a.getActiveLogin();if(!i)throw new Error("Unauthenticated: run `npx medplum login` to login");if(!i.project.reference)throw new Error("No current project to invite user to");let s=i.project.reference.split("/")[1],c={resourceType:n.role,firstName:e,lastName:t,email:o,sendEmail:!!n.sendEmail,admin:!!n.admin};await ro(s,c,a)});async function no(e,t){let n=e.getLogins().find(a=>a.project.reference?.includes(t));n?(await e.setActiveLogin(n),console.log(`Switched to project ${t}
|
|
8
|
+
`)):console.log(`Error: project ${t} not found. Make sure you are added as a user to this project`)}async function ro(e,t,o){try{await o.invite(e,t),t.sendEmail&&console.log("Email sent"),console.log("See your users at https://app.medplum.com/admin/users")}catch(n){console.log("Error while sending invite "+n)}}var yt=require("@medplum/core");var le=m("delete"),de=m("get"),ue=m("patch"),pe=m("post"),fe=m("put");le.argument("<url>","Resource/$id").action(async(e,t)=>{let o=await l(t);S(await o.delete(A(e,t)))});de.argument("<url>","Resource/$id").option("--as-transaction","Print out the bundle as a transaction type").action(async(e,t)=>{let n=await(await l(t)).get(A(e,t));t.asTransaction?S((0,yt.convertToTransactionBundle)(n)):S(n)});ue.arguments("<url> <body>").action(async(e,t,o)=>{let n=await l(o);S(await n.patch(A(e,o),ge(t)))});pe.arguments("<url> <body>").action(async(e,t,o)=>{let n=await l(o);S(await n.post(A(e,o),ge(t)))});fe.arguments("<url> <body>").action(async(e,t,o)=>{let n=await l(o);S(await n.put(A(e,o),ge(t)))});function ge(e){if(e)try{return JSON.parse(e)}catch{return e}}function A(e,t){let o=["admin/","auth/","fhir/R4"],{fhirUrlPath:n}=t;return o.some(a=>e.startsWith(a))?e:n?`${n}/${e}`:"fhir/R4/"+e}async function wt(e){try{let t=new ht.Command("medplum").description("Command to access Medplum CLI");t.version(q.MEDPLUM_VERSION),t.addCommand(H),t.addCommand(V),t.addCommand(de),t.addCommand(pe),t.addCommand(ue),t.addCommand(fe),t.addCommand(le),t.addCommand(gt),t.addCommand(lt),t.addCommand(rt),t.addCommand(ie),t.addCommand(se),t.addCommand(ce),t.addCommand(b),await t.parseAsync(e)}catch(t){console.error("Error: "+(0,q.normalizeErrorString)(t))}}async function Ct(){St.default.config(),await wt(process.argv)}require.main===module&&Ct().catch(e=>console.error("Unhandled error:",e));0&&(module.exports={main,run});
|
|
1498
9
|
//# sourceMappingURL=index.cjs.map
|