使用 REST 指令來部署應用程式:範例

下列範例說明如何使用 REST 指令來部署應用程式。

開始之前

此範例遵循指導教學建立簡式 helloWorld 部署中的步驟。 請先檢閱該指導教學,然後再嘗試遵循此範例。配置您的系統,如配置 helloWorld 指導教學系統中所述。適當時,請用值取代伺服器名稱、使用者名稱及密碼。

關於這項作業

這個範例顯示如何使用 Linux curl 程式來執行 REST 指令。您可以使用許多不同的程式或程式設計語言來執行 REST 指令。程式碼透過在指令行中指定使用者名稱及密碼,向伺服器進行鑑別。如果要進一步瞭解執行 REST 指令及鑑別,請參閱執行 REST 指令進行鑑別以執行 REST 指令

此範例中使用的伺服器是 server.example.com。 使用者是 nsmith,密碼是 passwd

程序

  1. 建立並匯入元件。
    1. 使用 createComponent PUT 指令來建立元件。
      curl -k -u nsmith:passwd
        https://server.example.com:8443/cli/component/create
        -X PUT -d @newComponent.json
      註: 您必須在單一行中輸入指令。這裡將指令分割在不同的行是為了便於說明。
      下列 JSON 檔代表元件:
      {
        "name": "helloWorld",  "description": "",
        "importAutomatically": false,
        "useVfs": true,
        "sourceConfigPlugin": "File System (Versioned)",
        "defaultVersionType": "FULL",
        "properties": {
          "FileSystemVersionedComponentProperties/basePath": "/home/user1/artifacts/shared/helloWorld"
        }
      }
      指令傳回與下列 JSON 資訊類似的結果:
      {
        "id":"cf355787-a772-4533-be45-3e169e9da6eb",
        "securityResourceId":"23504603-ab8c-4740-bc2d-d18c08f790e9",
        "name": "helloWorld",  "description":"",
        "created":1396878409753,
        "importAutomatically":false,
        "useVfs":true,
        "active":true,
        "integrationFailed":false,
        "deleted":false,
        "defaultVersionType":"FULL",
        "cleanupDaysToKeep":0,
        "cleanupCountToKeep":0,
        "tags":[],
        "user":"nsmith"
      }
    2. 使用 setComponentProperty PUT 指令來設定元件內容。
      curl -k -u nsmith:passwd 
        "https://server.example.com:8443/cli/
        component/propValue?component=helloWorld&name=helloHome&value=c%3A%5ChelloWorld" -X PUT
      指令傳回與下列 JSON 資訊類似的結果:
      {
        "id":"194b6e0c-3f30-4d9a-b3cf-dd61dd1a7328",
        "name":"helloHome",
        "value":"c:\\helloWorld",
        "secure":false
      }
    3. 使用 importVersions PUT 指令來匯入元件版本。
      curl -k -u nsmith:passwd 
        https://server.example.com:8443/cli/component/integrate
        -X PUT -d {"component":"helloWorld"}
      指令傳回下列結果:
      {}
  2. 使用 createComponentProcess PUT 指令來建立元件程序。
    curl -k -u nsmith:passwd
      https://server.example.com:8443/cli/componentProcess/create
      -X PUT -d @componentProcess.json
    下列 JSON 檔代表此元件程序:
    {
      "active": "true",
      "component": "helloWorld",
      "configActionType": "ADD",
      "defaultWorkingDir": "${p:resource/work.dir}/${p:component.name}",
      "description": "",
      "inventoryActionType": "ADD",
      "name": "helloProcess",
      "propDefs": [],
      "rootActivity": {
        "children": [
          {
            "allowFailure": false,
            "children": [],
            "commandName": "Delete Files and Directories",
            "impersonationUseSudo": false,
            "name": "Clean work space",
            "pluginName": "File Utils",
            "pluginVersion": 31,
            "properties":{
                "baseDir":".",
                "includes":"*",
                "excludes":"",
                "followSymlinks":"false",
                "caseSensitive":"true"
             },
            "showHidden": false,
            "type": "plugin",
            "useImpersonation": false
          },
          {
            "allowFailure": false,
            "children": [],
            "commandName": "Download Artifacts",
            "impersonationUseSudo": false,
            "name": "Download Artifacts",
            "pluginName": "IBM UrbanCode Deploy Versioned File Storage",
            "pluginVersion": 18,
            "properties":{
               "directoryOffset":".",
               "artifactSetBaseDir":"",
               "fileIncludePatterns":"**/*",
               "fileExcludePatterns":"",
               "syncMode":"true",
               "fullVerification":"true",
               "setFileExecuteBits":"false",
               "verifyFileIntegrity":"false",
               "repositoryUrl":"${p:server.url}/vfs",
               "repositoryId":"${p:component/code_station/repository}",
               "label":"${p:version.name}",
               "serverUrl":"${p:server.url}",
               "compId":"${p:component.id}",
               "resId":"${p:resource.id}",
               "charset":""
            },
            "showHidden": false,
            "type": "plugin",
            "useImpersonation": false
          },
          {
             "allowFailure":false,
             "children": [],
             "commandName":"Create .zip File",
             "impersonationUseSudo":false,
             "name":"Compress artifacts",
             "pluginName":"File Utils",
             "pluginVersion":31,
             "properties":{
                "zipName":"helloWorld.zip",
                "baseDir":".",
                "includes":"*",
                "excludes":"",
                "update":"false",
                "followSymlinks":"false",
                "caseSensitive":"true"
             },
            "showHidden": false,
            "type": "plugin",
            "useImpersonation": false
          },
          {
             "allowFailure":false,
             "children": [],
             "commandName":"Move Directory",
             "impersonationUseSudo":false,
             "name":"Deploy artifacts",
             "pluginName":"File Utils",
             "pluginVersion":31,
             "properties":{
                "sourceDir":".",
                "destDir":"${p:helloHome}/target",
                "includes":"*.zip",
                "excludes":"",
                "mapperRules":""
             },
            "showHidden": false,
            "type": "plugin",
            "useImpersonation": false
          },
          {
            "children": [],
            "name": "FINISH",
            "type": "finish"
          }
        ],
        "edges": [
          {
            "to": "Clean work space",
            "type": "ALWAYS",
            "value": ""
          },
          {
            "from": "Clean work space",
            "to": "Download Artifacts",
            "type": "ALWAYS",
            "value": ""
          },
          {
            "from": "Download Artifacts",
            "to": "Compress artifacts",
            "type": "ALWAYS",
            "value": ""
          },
          {
            "from": "Compress artifacts",
            "to": "Deploy artifacts",
            "type": "ALWAYS",
            "value": ""
          },
          {
            "from": "Deploy artifacts",
            "to": "FINISH",
            "type": "ALWAYS",
            "value": ""
          }
        ],
        "name": "GRAPH",
        "offsets": [
          {
            "name": "Clean work space",
            "x": "-65",
            "y": "90"
          },
          {
            "name": "Download Artifacts",
            "x": "-70",
            "y": "210"
          },
          {
            "name": "Compress artifacts",
            "x": "-70",
            "y": "290"
          },
          {
            "name": "Deploy artifacts",
            "x": "-65",
            "y": "370"
          },
          {
            "name": "FINISH",
            "x": "-60",
            "y": "450"
          }
        ],
        "type": "graph"
      },
      "status": "Active",
      "takesVersion": "true"
    }
    指令傳回與下列 JSON 資訊類似的結果:
    {
      "id":"12f65517-5fc1-44ff-a58b-2401cfba8f2f",
      "name":"helloProcess",
      "description":"",
      "defaultWorkingDir":"${p:resource\/work.dir}\/${p:component.name}",
      "takesVersion":true,
      "inventoryActionType":"ADD",
      "status":"Active",
      "active":true,
      "versionCount":1,
      "version":1,
      "commit":0,
      "path":"components\/cf355787-a772-4533-be45-3e169e9da6eb\/processes\/12f65517-5fc1-44ff-a58b-2401cfba8f2f"
    }
  3. 建立應用程式,然後將元件與該應用程式建立關聯。
    1. 使用 createApplication PUT 指令來建立應用程式。
      curl -k -u nsmith:passwd
        https://server.example.com:8443/cli/application/create
        -X PUT -d @newApplication.json
      下列 JSON 檔代表應用程式:
      {
        "name": "hello Application",
        "description": "",
        "notificationScheme": "None",
        "enforceCompleteSnapshots": "false"
      }
      指令傳回與下列 JSON 資訊類似的結果:
      {
        "id":"932452cc-3562-4545-8de5-c57aa7efbcc4",
        "securityResourceId":"2bc72218-dbfe-4c11-b16f-752b7ca63d06",
        "name":"hello Application",
        "description":"",
        "created":1396882026943,
        "enforceCompleteSnapshots":false,
        "active":true,
        "tags":[],
        "user":"nsmith"
      }
    2. 使用 addComponentToApp PUT 指令將元件與該應用程式建立關聯。
      curl -k -u nsmith:passwd 
        "https://server.example.com:8443/cli/
        application/addComponentToApp?component=helloWorld&application=hello%20Application"
        -X PUT
      指令傳回下列結果:
      {}
  4. 建立環境,並將其與資源建立關聯。
    1. 使用 createEnvironment PUT 指令來建立環境。
      curl -k -u nsmith:passwd 
        "https://server.example.com:8443/cli/environment/createEnvironment?application=hello%20Application&name=helloDeploy"
        -X PUT
      指令傳回環境的 ID,該 ID 類似於下列字串:
      2142c0e3-7ebe-4a2f-9e0d-14ccacc70a79
    2. 使用 addEnvironmentBaseResources PUT 指令將您先前配置的資源與該環境建立關聯。
      curl -k -u nsmith:passwd 
        "https://server.example.com:8443/cli/environment/addBaseResource?application=hello%20Application&environment=helloDeploy&resource=%2FhelloWorld%20Tutorial"
        -X PUT
      如果資源未與該環境正確關聯,則此指令不傳回結果。
    3. 使用 createResource PUT 指令將 helloWorld 元件對映至代理程式資源。
      curl -k -u nsmith:passwd 
        https://server.example.com:8443/cli/resource/resource 
        -X PUT -d @componentResource.json
      下列 JSON 檔代表元件資源:
      {
        "roleId":"09456abd-65e3-469e-94c6-a6a873b971a8",
        "name": "helloWorld",  "description":"",
        "inheritTeam":"true",
        "useImpersonation":"false",
        "roleProperties":{},
        "parentId":"b2ff919e-b4c0-4d13-8973-eff44e4835e2",
        "teamMappings":[]
      }
      指令傳回與下列 JSON 資訊類似的結果:
      {
        "id":"ba958487-fb86-4290-9240-da766af37963",
        "securityResourceId":"66685b67-21ab-411d-99bb-f577a74d88b3",
        "name": "helloWorld",  "path":"\/helloWorld Tutorial\/fit-vm11-157\/helloWorld",
        "active":true,
        "description":"",
        "inheritTeam":true,
        "impersonationPassword":"****",
        "impersonationUseSudo":false,
        "impersonationForce":false,
        "type":"subresource",
        "status":"ONLINE",
        "hasAgent":true,
        "tags":[]
      }
    4. 使用 addTagToResource PUT 指令將標籤新增至元件資源。
      curl -k -u nsmith:passwd
        "https://server.example.com:8443/cli/
        resource/tag?resource=ba958487-fb86-4290-9240-da766af37963&tag=blueCycle&color=0000FF"
        -X PUT
      如果未正確地標記該元件,則此指令不傳回結果。
  5. 使用 createApplicationProcess PUT 指令來建立應用程式程序。
    curl -k -u nsmith:passwd
      https://server.example.com:8443/cli/application/create
      -X PUT -d @applicationProcess.json
    下列 JSON 檔代表應用程式程序。應用程式 ID 與步驟 3 中傳回的 ID 相同。
    {
      "application": "932452cc-3562-4545-8de5-c57aa7efbcc4",
      "description": "",
      "inventoryManagementType": "AUTOMATIC",
      "name": "hello App Process",
      "offlineAgentHandling": "PRE_EXECUTION_CHECK",
      "rootActivity": {
        "children": [
          {
            "children": [{
              "children": [{
                "allowFailure": false,
                "children": [],
                "componentName": "helloWorld",
                "componentProcessName": "helloProcess",
                "name": "Deploy helloWorld",
                "properties": {},
                "type": "componentProcess"
              }],
              "componentName": "helloWorld",
              "name": "inventoryVersionCheck",
              "status": "Active",
              "type": "inventoryVersionDiff"
            }],
            "componentName": "helloWorld",
            "failFast": "false",
            "maxIteration": "-1",
            "name": "Deploy helloWorld",
            "runOnlyOnFirst": "false",
            "type": "componentEnvironmentIterator"
          },
          {
            "children": [],
            "name": "FINISH",
            "type": "finish"
          }
        ],
        "edges": [
          {
            "to": "Deploy helloWorld",
            "type": "ALWAYS",
            "value": ""
          },
          {
            "from": "Deploy helloWorld",
            "to": "FINISH",
            "type": "ALWAYS",
            "value": ""
          }
        ],
        "name": "GRAPH",
        "offsets": [
          {
            "name": "Deploy helloWorld",
            "x": "-35",
            "y": "210"
          },
          {
            "name": "FINISH",
            "x": "0",
            "y": "420"
          }
        ],
        "propDefs": [],
        "type": "graph"
      }
    }
    指令傳回與下列 JSON 資訊類似的結果:
    {
      "id":"37d563ce-7885-48f1-b8c0-f79046c0f024",
      "name":"hello App Process",
      "description":"",
      "active":true,
      "inventoryManagementType":"AUTOMATIC",
      "offlineAgentHandling":"PRE_EXECUTION_CHECK",
      "versionCount":1,
      "version":1,
      "commit":0,
      "path":"applications\/932452cc-3562-4545-8de5-c57aa7efbcc4\/processes\/37d563ce-7885-48f1-b8c0-f79046c0f024"
    }
  6. 使用 requestApplicationProcess PUT 指令來部署應用程式。
    curl -k -u nsmith:passwd
      https://server.example.com:8443/cli/applicationProcessRequest/request
      -X PUT -d @applicationDeploy.json
    下列 JSON 檔代表部署要求:
    {
      "application": "hello Application",
      "applicationProcess": "hello App Process",
      "environment": "helloDeploy",
      "versions": [{
        "version": "1.0",
        "component": "helloWorld"
      }]
    }
    指令傳回與下列 JSON 資訊類似的結果:
    {"requestId":"bffff82f-ff69-4c07-b010-26ca3f21be39"}

意見