import { dialog , OpenDialogOptions , Menu } from "electron" ; import { projects_list , projects_window , workspace_window , setCurrentWorkspace , getCurrentWorkspace , workspace_menu } from "../global" ; import { AssignClassWindow } from "../windows/assign_class/window" ; import { CreateResourceWindow } from "../windows/create_resource/window" ; import { CreateEnvironmentWindow } from "../windows/create_environment/window" ; import { CreateProjectWindow } from "../windows/create_project/window" ; import { ProgressWindow } from "../windows/progress/window" ; import { ManagePropertyHierarchyWindow } from "../windows/manage_property_hierarchy/window" ; import { ManageEYamlWindow } from "../windows/manage_eyaml/window" ; import { EnterPasswordWindow } from "../windows/enter_password/window" ; import { ProjectModel } from "../projects" ; import { isDirectory , listFiles } from "../async" ; import { Workspace } from "../puppet/workspace" ; import { Environment } from "../puppet/environment" ; import register from "electron-ipc-tunnel/server" ; import { IpcAPI } from "./api" ; import { WorkspaceSettings } from "../puppet/workspace_settings" ; import { Ruby } from "../puppet/ruby" ; import { WorkspaceError } from "../puppet/util" ; import { ClassDump , ResourceDump , EnvironmentTreeDump } from "./objects" ; export class IpcServer implements IpcAPI { public async addProject ( path ) { return await projects_list . addProject ( path ) ; } public async openProject ( path , offline ) { const project = projects_list . getProject ( path ) ; if ( project == null ) throw new Error ( "No such project: " + path ) ; if ( offline ) { project . workspace . setOfflineMode ( ) ; } setCurrentWorkspace ( project . workspace ) ; workspace_window . show ( path ) ; Menu . setApplicationMenu ( workspace_menu ) ; projects_window . close ( ) ; } public async createProject ( ) { const window = new CreateProjectWindow ( ) ; const [ path , env ] = await window . show ( ) ; if ( ! await isDirectory ( path ) ) { throw new Error ( "Please choose an empty directory" ) ; } const files = await listFiles ( path ) ; if ( files . length > 0 ) { throw new Error ( "Please choose an empty directory" ) ; } if ( ! await projects_list . createProject ( path , env ) ) { throw new Error ( "Failed to create a new project" ) ; } await this . openProject ( path , false ) ; } public async getProjectList ( ) { const projects = projects_list . list ; const result = [ ] ; for ( let project of projects ) { if ( ! project . workspace ) continue ; result . push ( { "name" : project . workspace . name , "path" : project . path } ) ; } return result ; } public async getEnvironmentList ( ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) { return [ ] ; } const names = [ ] ; for ( const env of await workspace . listEnvironments ( ) ) { names . push ( env . name ) ; } return names ; } public async getEnvironmentTree ( name ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) { return null ; } const environment = await workspace . getEnvironment ( name ) ; if ( environment == null ) { return null ; } return await environment . tree ( ) ; } public async findFile ( localPath ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) { return null ; } const node = await workspace . findFile ( localPath ) ; if ( node == null ) { return null ; } return node . dump ( ) ; } public async acquireNodeClass ( environment , certname , className ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return null ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return null ; const node = env . getNode ( certname ) ; if ( node == null ) return null ; return await node . dumpClass ( className ) ; } public async acquireNodeResource ( environment , certname , definedTypeName , title ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return null ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return null ; const node = env . getNode ( certname ) ; if ( node == null ) return null ; return await node . dumpResource ( definedTypeName , title ) ; } public async setNodeProperty ( environment , certname , hierarchy , property , value ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return null ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return null ; const node = env . getNode ( certname ) ; if ( node == null ) return null ; return await node . setProperty ( hierarchy , property , value ) ; } public async encryptNodeClassProperty ( environment , certname , hierarchy , className , propertyName ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return null ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return null ; const node = env . getNode ( certname ) ; if ( node == null ) return null ; return await node . encryptNodeProperty ( hierarchy , className , propertyName ) ; } public async setNodeClassProperty ( environment , certname , hierarchy , className , propertyName , value ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return null ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return null ; const node = env . getNode ( certname ) ; if ( node == null ) return null ; return await node . setClassProperty ( className , hierarchy , propertyName , value ) ; } public async hasNodeClassProperty ( environment , certname , className , propertyName ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return false ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return false ; const node = env . getNode ( certname ) ; if ( node == null ) return false ; return await node . hasClassProperty ( className , propertyName ) ; } public async removeNodeProperty ( environment , certname , hierarchy , propertyName ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return false ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return false ; const node = env . getNode ( certname ) ; if ( node == null ) return false ; return await node . removeProperty ( hierarchy , propertyName ) ; } public async removeNodeClassProperty ( environment , certname , hierarchy , className , propertyName ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return false ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return false ; const node = env . getNode ( certname ) ; if ( node == null ) return false ; return await node . removeClassProperty ( className , hierarchy , propertyName ) ; } public async setNodeResourceProperty ( environment , certname , hierarchy , key , definedTypeName , title , propertyName , value ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return false ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return false ; const node = env . getNode ( certname ) ; if ( node == null ) return false ; return await node . setResourceProperty ( definedTypeName , title , hierarchy , key , propertyName , value ) ; } public async removeNodeResourceProperty ( environment , certname , hierarchy , key , definedTypeName , title , propertyName ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return false ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return false ; const node = env . getNode ( certname ) ; if ( node == null ) return false ; return await node . removeResourceProperty ( definedTypeName , title , hierarchy , key , propertyName ) ; } public async removeNodeClassProperties ( environment , certname , className ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return false ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return false ; const node = env . getNode ( certname ) ; if ( node == null ) return false ; } public async getClassInfo ( env ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) { return null ; } const environment = await workspace . getEnvironment ( env ) ; if ( environment == null ) { return null ; } return environment . getClassInfo ( ) ; } public async initWorkspace ( ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) { return null ; } await workspace . init ( ( progress ) => { workspace_window . browserWindow . webContents . send ( "refreshWorkspaceProgress" , progress ) ; } , ( text , showProgress ) => { workspace_window . browserWindow . webContents . send ( "refreshWorkspaceCategory" , text , showProgress ) ; } ) ; } public showOpenDirectoryDialog ( defaultPath ? , mode = [ 'openDirectory' ] ) { return new Promise < string > ( ( resolve , reject ) => { const options = { 'defaultPath' : defaultPath , 'properties' : mode } ; dialog . showOpenDialog ( options , ( filePaths ) => { if ( filePaths ) { resolve ( filePaths [ 0 ] ) ; } else { resolve ( null ) ; } } ) ; } ) ; } public async getCurrentWorkspacePath ( ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return null ; return workspace . path ; } public async assignNewHieraClass ( environment , certname , includeName , hierarchy ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return null ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return null ; const node = env . getNode ( certname ) ; if ( node == null ) return null ; const nodeHierarchy = node . hierarchy . get ( hierarchy ) ; if ( nodeHierarchy == null ) return ; const window = new AssignClassWindow ( environment , certname , hierarchy , { "name" : nodeHierarchy . entry . name , "path" : nodeHierarchy . path } , includeName ) ; const className = await window . show ( ) ; if ( ! className ) return null ; await node . assignClass ( includeName , className , hierarchy ) ; return className ; } public async managePropertyHierarchy ( environment , certname , property , _constructor ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return null ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return null ; const node = env . getNode ( certname ) ; if ( node == null ) return null ; const hierarchy = [ ] ; for ( const entry of node . hierarchy . hierarhy ) { hierarchy . push ( { name : entry . entry . name , path : entry . path , eyaml : entry . eyaml , defined : entry . isPropertyDefined ( property ) } ) ; } const window = new ManagePropertyHierarchyWindow ( environment , certname , property , hierarchy , _constructor ) ; return await window . show ( ) ; } public async chooseDefinedType ( environment , certname ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return null ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return null ; const node = env . getNode ( certname ) ; if ( node == null ) return null ; const window = new CreateResourceWindow ( environment , certname ) ; const result = await window . show ( ) ; return result ; } public async createNewResourceToNode ( environment , certname , key , hierarchy , definedTypeName , title ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return false ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return false ; const node = env . getNode ( certname ) ; if ( node == null ) return false ; return await node . createResource ( key , hierarchy , definedTypeName , title ) ; } public async removeHieraClassFromNode ( environment , certname , key , hierarchy , className ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return ; const node = env . getNode ( certname ) ; if ( node == null ) return ; await node . removeClass ( key , className , hierarchy ) ; } public async removeResourceFromNode ( environment , certname , key , hierarchy , definedTypeName , title ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return ; const node = env . getNode ( certname ) ; if ( node == null ) return ; return await node . removeResource ( key , hierarchy , definedTypeName , title ) ; } public async removeResourcesFromNode ( environment , certname , key , hierarchy , definedTypeName ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return ; const node = env . getNode ( certname ) ; if ( node == null ) return ; return await node . removeResources ( key , hierarchy , definedTypeName ) ; } public async removeAllResourcesFromNode ( environment , certname , key , hierarchy ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return ; const node = env . getNode ( certname ) ; if ( node == null ) return ; return await node . removeAllResources ( key , hierarchy ) ; } public async renameNodeResource ( environment , certname , key , hierarchy , definedTypeName , title , newTitle ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return false ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return false ; const node = env . getNode ( certname ) ; if ( node == null ) return false ; return await node . renameResource ( key , hierarchy , definedTypeName , title , newTitle ) ; } public async removeHieraClasses ( environment , certname , includeName ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return null ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return null ; const node = env . getNode ( certname ) ; if ( node == null ) return null ; } public async searchClasses ( environment , certname , search ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return [ ] ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return [ ] ; const node = env . getNode ( certname ) ; if ( node == null ) return [ ] ; return await node . env . searchClasses ( search ) ; } public async searchDefinedTypes ( environment , certname , search ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return [ ] ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return [ ] ; const node = env . getNode ( certname ) ; if ( node == null ) return [ ] ; return await node . env . searchDefinedTypes ( search ) ; } public async acquireNodeFacts ( environment , certname ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return null ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return null ; const node = env . getNode ( certname ) ; if ( node == null ) return null ; } public async invalidateNode ( environment , certname ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return ; const node = env . getNode ( certname ) ; if ( node == null ) return ; await node . invalidate ( ) ; } public async invalidateNodeClass ( environment , certname , className ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return ; const node = env . getNode ( certname ) ; if ( node == null ) return ; await node . invalidateClass ( className ) ; } public async isNodeClassValid ( environment , certname , className ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return false ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return false ; const node = env . getNode ( certname ) ; if ( node == null ) return false ; return await node . isClassValid ( className ) ; } public async isNodeDefinedTypeValid ( environment , certname , definedTypeName , title ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return false ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return false ; const node = env . getNode ( certname ) ; if ( node == null ) return false ; return await node . isResourceValid ( definedTypeName , title ) ; } public async createFolder ( path , name ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return ; const directory = await workspace . findFolder ( path ) ; if ( directory == null ) return ; return await directory . createFolder ( name ) != null ; } public async removeEnvironment ( name ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return false ; return await workspace . removeEnvironment ( name ) ; } public async getGlobalModules ( ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return false ; return await workspace . getGlobalModules ( ) ; } public async getEnvironmentModules ( env ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return false ; return await workspace . getEnvironmentModules ( env ) ; } public async createEnvironment ( ) { const window = new CreateEnvironmentWindow ( ) ; const env = await window . show ( ) ; if ( env == null ) return ; const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return ; return await workspace . createEnvironment ( env ) ; } public async installModules ( ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return ; const progress = new ProgressWindow ( ) ; await progress . show ( ) ; try { const hadAny = await workspace . installModules ( ( text ) => { progress . notifyProgressUpdate ( text ) ; } ) ; if ( hadAny ) { progress . close ( ) ; } else { throw new Error ( "Puppetfile wasn't found. Please place Puppetfile in the root of your project, or in the environment folder." ) ; } } catch ( e ) { if ( e . hasOwnProperty ( "title" ) ) { progress . notifyProgressError ( e . title , e . message ) ; } else { progress . notifyProgressError ( "Failed to process the workspace" , e . message ) ; } } } public async publishCSR ( server , certname ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) { throw new WorkspaceError ( "Failed to publich CRS" , "No workspace" ) ; } return await workspace . publishCSR ( server , certname ) ; } public async downloadSignedCertificate ( ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) { throw new WorkspaceError ( "Failed to publich CRS" , "No workspace" ) ; } await workspace . downloadSignedCertificate ( ) ; } public async checkAuthentication ( ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) { throw new WorkspaceError ( "Failed to publich CRS" , "No workspace" ) ; } await workspace . checkAuthentication ( ) ; } public async ignoreNode ( cername ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) { throw new WorkspaceError ( "Failed to publich CRS" , "No workspace" ) ; } await workspace . addNodeToIgnoreList ( cername ) ; } public async clearIgnoreNodeList ( ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) { throw new WorkspaceError ( "Failed to publich CRS" , "No workspace" ) ; } return await workspace . clearNodeIgnoreList ( ) ; } public async isEYamlKeysImported ( environment , certname , hierarchy ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return false ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return false ; const node = env . getNode ( certname ) ; if ( node == null ) return false ; return node . isEYamlKeysImported ( hierarchy ) ; } public async manageEYamlKeys ( environment , certname , hierarchy ) { const workspace = getCurrentWorkspace ( ) ; if ( workspace == null ) return false ; const env = await workspace . getEnvironment ( environment ) ; if ( env == null ) return false ; const node = env . getNode ( certname ) ; if ( node == null ) return false ; const hasPublicKey = node . hasEYamlPublickKey ( hierarchy ) ; const publicKey = hasPublicKey ? node . getEYamlPublicKey ( hierarchy ) : null ; const window = new ManageEYamlWindow ( publicKey , hasPublicKey ) ; const updatedPublicKey = await window . show ( ) ; if ( updatedPublicKey != null ) { return await node . updateEYamlKeys ( hierarchy , updatedPublicKey ) ; } return false ; } } const server = new IpcServer ( ) ; for ( const methodName of Object . getOwnPropertyNames ( Object . getPrototypeOf ( server ) ) ) { if ( methodName == "constructor" ) continue ; register ( methodName , ( receive , ... args ) => { return ( server as any ) [ methodName ] . apply ( server , args ) ; } ) ; }