Create and Read
Create
Create adds a new file in the current DropZone. If an optional stream is specified, then the file is created from the stream content.
create requires named parameters:
Parameter | Description |
---|---|
file | Mandatory. The name of the file to create in the current DropZone. |
stream | Optional. The stream to use to populate the file. If the stream parameter is absent, the file is created with zero size. |
Once the file is created it is marked for deployment. It will be pushed to the target Endpoint(s) during any subsequent transfer operation.
Create Example
Creates a config file and push it to the target _Endpoint_s. This should be placed into a Custom Action in a Component:
using DropZone 'demozone1' {
using stream $demostream {
echo "# This is a config file";
echo "[p1]";
echo "p1=$p1";
echo "p2=$p2";
}
create(file: "config.file", stream: $demostream);
transfer; // copies file in DropZone to target Endpoint(s)
}
Read
Reads a file in the current DropZone and writes its content into a stream.
read takes at most two parameters:
Parameter | Description |
---|---|
file | Mandatory. The name of the file to read. |
stream | Optional when read is used inside a using stream block, mandatory otherwise. Specifies the name of an existing stream. |
NOTE: If stream is not specified, then the file content is written into the stream used by the enclosing using stream block. If stream is not specified and read is used outside a using stream block a runtime error occurs.
Example 1
Checkout a file from a repository, read its content into a stream, convert it to base64, create a new file containing the base64 encoded file and transfer it to the target Endpoint(s). This should be placed into a Component as a Custom Action.
using DropZone 'dp' {
using stream $configzip; // create the stream
checkout(repository: 'DPRep', pattern: 'demo1.zip');
read(file: 'demo1.zip', stream: $configzip);
set b64 = ${configzip.base64encode()};
// create a new file
create(file: "demo2.b64", stream: $configzip);
delete(file: "demo1.zip"); // Remove original file
transfer; // transfer demo2.b64 to target
}
Example 2
Create new users in DeployHub Pro from a file. Assume the file is constructed like this:
jdoe|John Doe|[email protected]
bsmith|Bill Smith|[email protected]
The following code, when executed, will read this file and call DeployHub Pro’s own API in order to add the users to the database:
set APIURL="http://mac:8080/dmadminweb/API/new/user/";
set session={"JSESSIONID": "$JSESSIONID"};
using DropZone 'users' {
using stream $users; // create the stream
checkout(repository: 'demorep2', pattern: 'users.txt');
read(file: 'users.txt', stream: $users);
foreach(line: $users) {
set elems = ${line.split("|")};
set attrs = {
"domain": "Openmake\_Demo",
"realname": "${elems[1]}",
"email": "${elems[2]}",
"pw": "welc0me" // initial password
};
set res = restful\_post($APIURL+${elems[0]},$attrs,$session);
if (${res.success}) {
echo "Added user ${elems[0]} okay";
} else {
echo "${res.error}";
}
}
Result
Added user jdoe okay Added user bsmith okay
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.