Skip to content

Commit

Permalink
bak
Browse files Browse the repository at this point in the history
  • Loading branch information
Mārtiņš Tālbergs committed Sep 28, 2020
1 parent bc585bd commit 73c035f
Show file tree
Hide file tree
Showing 50 changed files with 1,149 additions and 7 deletions.
158 changes: 158 additions & 0 deletions bin/util/zbx-jira-edit
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#!/usr/bin/env php
<?php

[, $json_file, $issuenr] = $argv;
[
'fields' => [
'summary' => $title,
'description' => $description,
'comment' => [
'comments' => $comments,
],
],
] = json_decode(file_get_contents($json_file), true);

function marshal(array $lines): string {
return implode("\r\n", array_map(fn($l) => substr($l, 4), $lines));
}

function unmarshal(string $str): string {
return implode("\n", array_map(fn($l) => " $l", explode("\r\n", $str)));
}

$orig_comments = [];
$comments_str = '';
foreach ($comments as $comment) {
['body' => $body, 'id' => $id, 'author' => ['displayName' => $author]] = $comment;
$meta = [
'author' => $author,
'id' => $id,
'public' => true,
];

if (array_key_exists('visibility', $comment) && $comment['visibility']['value'] === 'Developers') {
$meta['public'] = false;
}

$orig_comments[$id] = ['body' => $body, 'id' => $id, 'public' => $meta['public']];

$comments_str .= implode(PHP_EOL, [
PHP_EOL,
'<' . json_encode($meta),
unmarshal($body),
'/>',
]);
}

$mdfile = $json_file . '_live';
$mdstr = implode(PHP_EOL, [
$title,
'<',
unmarshal($description),
'/>',
$comments_str
]);

file_put_contents($mdfile, $mdstr);

system(getenv('EDITOR') . ' ' . $mdfile . ' > `tty`', $exit_code);

if ($exit_code != 0) {
fwrite(STDERR, 'Editing was canceled.' . PHP_EOL);
exit(1);
}

$newmdfile = explode("\n", file_get_contents($mdfile));
$new_title = array_shift($newmdfile);

$ticket_fields = [];
if ($new_title !== $title) {
$ticket_fields['summary'] = $new_title;
}

function fetch_section(array &$lines): array {
$attr_line = array_shift($lines);
while (($attr_line[0] ?? '') !== '<') {
$attr_line = array_shift($lines);
if ($attr_line === null) {
$attr_line = '<{}';
break;
}
}

$end = array_search('/>', $lines);
$sect = array_splice($lines, 0, $end + 1);
array_pop($sect);

$attr = json_decode(substr($attr_line, 1) ?: '{}', true);

return [marshal($sect), $attr];
}

[$new_description] = fetch_section($newmdfile);

if ($new_description !== $description) {
$ticket_fields['description'] = $new_description;
}

if ($ticket_fields) {
$basicauth = sprintf('%s:%s', getenv('JIRAUSER'), getenv('JIRAPASS'));
$apiurl = sprintf('%s/rest/api/latest/issue/%s', getenv('JIRAURL'), $issuenr);
/* `curl -u $basicauth --data 'json_encode($ticket_fields)' -X PUT -H "Content-Type: application/json" $apiurl`; */
// ^ THIS WORKS
echo 'Updating ticket_fields: '.json_encode($ticket_fields, JSON_PRETTY_PRINT).PHP_EOL;
}

while ($newmdfile) {
[$comment, $attr] = fetch_section($newmdfile);

// ==========MANUAL==========
// https://docs.atlassian.com/software/jira/docs/api/REST/8.11.0/#api/2/issue-getComments
//
// UPDATING
//
// PUT /rest/api/2/issue/$issuenr/comment/$attr['id']
/* { */
/* "body": "ssssssssss", */
/* "visibility": { */
/* "type": "role", */
/* "value": "Developers" */
/* } */
/* } */

// POST /rest/api/2/issue/$issuenr/comment
//
/* { */
/* "body": "ssssssssss", */
/* "visibility": { */
/* "type": "role", */
/* "value": "Developers" */
/* } */
/* } */

if (array_key_exists('id', $attr)) {
$orig_comment = $orig_comments[$attr['id']];

if ($orig_comment['body'] !== $comment) {
echo $comment.PHP_EOL;
echo '^^ updating ^^'.PHP_EOL;

/* $basicauth = sprintf('%s:%s', getenv('JIRAUSER'), getenv('JIRAPASS')); */
/* $apiurl = sprintf('%s/rest/api/latest/issue/%s', getenv('JIRAURL'), $ticket); */

/* $json = `curl -u $basicauth -X PUT -H "Content-Type: application/json" $apiurl`; */
/* echo "\n{$json}\n"; */
}

if ($orig_comment['public'] !== $attr['public']) {
echo '^^ updating visibility ^^'.PHP_EOL;
}
} else {
if (trim($comment)) {
echo $comment.PHP_EOL;
echo '^^ create ^^'.PHP_EOL;
}
}
}

exit(0);
45 changes: 45 additions & 0 deletions bin/util/zbx-jira-fetch
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env php
<?php

preg_match('/.*?\/feature\/(\w+\-\d+).*$/', getcwd(), $matches);

if (!$matches) {
fwrite(STDERR, 'Cannot determine REF' . PHP_EOL);
exit(3);
}

[, $ticket] = $matches;

$basicauth = sprintf('%s:%s', getenv('JIRAUSER'), getenv('JIRAPASS'));
$apiurl = sprintf('%s/rest/api/latest/issue/%s', getenv('JIRAURL'), $ticket);

$json = `curl -u $basicauth -X GET -H "Content-Type: application/json" $apiurl`;
/* $json = file_get_contents('/home/ada/.zbx-box/jira-ticket.json'); */

if ($json === null) {
fwrite(STDERR, 'Could not fetch from ' . $apiurl . PHP_EOL);
exit(4);
} else {
fwrite(STDERR, 'Fetched ' . $apiurl . PHP_EOL);
}

$obj = json_decode($json);
if (json_last_error() !== JSON_ERROR_NONE) {
fwrite(STDERR, $json . PHP_EOL);
fwrite(STDERR, '^ not a JSON' . PHP_EOL);
exit(5);
}

$WORKTREE = getenv('WORKTREE');
`mkdir -p $WORKTREE/jira`;

$jirafile = $WORKTREE . '/jira/' . $ticket;
$written = file_put_contents($jirafile, $json);
if ($written === false || $written !== strlen($json)) {
fwrite(STDERR, $json . PHP_EOL);
fwrite(STDERR, '^ not saved to ' . $jirafile . PHP_EOL);
exit(6);
}

fwrite(STDOUT, $jirafile);
exit(0);
3 changes: 3 additions & 0 deletions bin/util/zbx-jira-sync
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env php
<?php
echo "SYNCING";
19 changes: 19 additions & 0 deletions bin/zbx,Jira-Ticket
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
### Usage: $0
## Allows to edit/add jira comments.
## Allows to view jira ticket description.
##~
source zbx-script-header
source zbx-get-ref

issuenr=$(grep -oP "\w+\-\d+" <<< $REF)
issuenr="ZBXNEXT-6107"

issuefile=$WORKTREE/jira/$issuenr
if [ ! -f $issuefile ]; then
issuefile=$(zbx-jira-fetch $issuenr)
fi

echo $issuefile

zbx-jira-edit $issuefile $issuenr && zbx-jira-sync
3 changes: 2 additions & 1 deletion bin/zbx,api
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,15 @@ source zbx-script-header
URL=$(zbx,web -n)
[ -z $URL ] && exit 4
URL="${URL}api_jsonrpc.php"
# URL="https://balance.localhost/ZBXNEXT-1660-5.1/api_jsonrpc.php"

METHOD=${1:-host.get}
USERNAME=${2:-Admin}
PASSWORD=${3:-zabbix}

TOKEN=$(get_token "${URL}" "${USERNAME}" "${PASSWORD}")

PARAMS_FILE=/tmp/${METHOD}.json
PARAMS_FILE=$PWD/${METHOD}.json
if [ ! -e "${PARAMS_FILE}" ];then
echo -e '{\n "output":[]\n}' > $PARAMS_FILE
fi
Expand Down
41 changes: 40 additions & 1 deletion bin/zbx,box
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ compose-service() {

name=$1
case $name in
php54-fpm | php72-fpm | php74-fpm )
php54-fpm | php56-fpm | php70-fpm | php72-fpm | php74-fpm )
zbx-compose $name \
--user $(id -u):$(id -g) \
--add-host localhost:192.168.1.101 \
-v /home/ada/zabbix-dev/feature/ZBXNEXT-1660-5.1/certs:/certs \
--volume "$ZBX_BOX_ROOT"/cfg/$name/php.ini:/usr/local/etc/php/conf.d/zabbix.ini \
--volume "$ZBX_BOX_ROOT"/cfg/$name/fpm.conf:/usr/local/etc/php-fpm.d/zabbix.conf \
--volume "$ZBX_BOX_ROOT"/decoration:/decoration
Expand Down Expand Up @@ -72,11 +74,19 @@ compose-service() {
--shm-size=4g \
--rm
;;
mysql-5)
zbx-compose $name \
--volume zbx-box-db-mysql-5:/var/lib/mysql \
--volume "$ZBX_BOX_ROOT"/cfg/$name:/etc/mysql/conf.d
;;
mysql-8)
zbx-compose $name \
--volume zbx-box-db-mysql-8:/var/lib/mysql \
--volume "$ZBX_BOX_ROOT"/cfg/$name:/etc/mysql/conf.d
;;
ssh-server)
zbx-compose $name
;;
snmp-simulator)
zbx-compose $name
;;
Expand All @@ -101,6 +111,35 @@ compose-service() {
zbx-compose $name \
--publish 8025:8025
;;
traefik)
# TODO: audit logs to stdout
docker run \
--detach \
--rm \
--network zbx-box \
--label zbx-box=traefik \
-p 8090:8080 \
-p 8070:8070 \
--volume "$ZBX_BOX_ROOT"/cfg/$name:/etc/traefik \
--name zbx-box-traefik \
traefik
;;
vault)
# TODO: audit logs to stdout
docker run \
--rm \
--network zbx-box \
--volume zbx-box:/var/www/html \
--label zbx-box=vault \
--detach \
--cap-add=IPC_LOCK \
-e 'VAULT_DEV_ROOT_TOKEN_ID=zabbix' \
-e 'VAULT_DEV_LISTEN_ADDRESS=0.0.0.0:1234' \
-e 'VAULT_LOG_LEVEL=trace' \
--publish 1234:1234 \
--name zbx-box-vault \
vault
;;
* )
>&2 echo "Unknown service '$name'";
;;
Expand Down
37 changes: 34 additions & 3 deletions bin/zbx,db
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,13 @@ postgres() {
# files+=(/var/www/html/$REF/ui/tests/selenium/data/data_test.sql)
fi

if [ ! -z $integration_data ];then
files+=(/var/www/html/$REF/frontends/php/tests/integration/data/data_test.sql)
fi

if [ ! -z $apijson ];then
files+=(/var/www/html/$REF/ui/tests/api_json/data/data_test.sql)
# files+=(/var/www/html/$REF/frontends/php/tests/api_json/data/data_test.sql)
# files+=(/var/www/html/$REF/ui/tests/api_json/data/data_test.sql)
files+=(/var/www/html/$REF/frontends/php/tests/api_json/data/data_test.sql)
fi

(
Expand All @@ -72,6 +76,29 @@ postgres() {
| zbx-util-color -H "psql -q -U postgres \"$DBNAME\""
}

mysql-5() {
zbx-util-color -H "DROP DATABASE IF EXISTS \"$DBNAME\""
docker exec zbx-box-mysql-5 mysql -uroot -pzabbix -e "DROP DATABASE IF EXISTS \`$DBNAME\`"
zbx-util-color -H "CREATE DATABASE \"$DBNAME\""
docker exec zbx-box-mysql-5 mysql -uroot -pzabbix -e "CREATE DATABASE \`$DBNAME\` CHARACTER SET utf8 COLLATE utf8_bin;"
zbx-util-color -H "mysql/schema.sql"
docker exec zbx-box-mysql-5 mysql -uroot -pzabbix -D "$DBNAME" -e "source /var/www/html/$REF/database/mysql/schema.sql"
zbx-util-color -H "mysql/images.sql"
docker exec zbx-box-mysql-5 mysql -uroot -pzabbix -D "$DBNAME" -e "source /var/www/html/$REF/database/mysql/images.sql"
zbx-util-color -H "mysql/data.sql"
docker exec zbx-box-mysql-5 mysql -uroot -pzabbix -D "$DBNAME" -e "source /var/www/html/$REF/database/mysql/data.sql"

if [ ! -z $selenium ];then
zbx-util-color -H "selenium/data/data_test.sql"
docker exec zbx-box-mysql-5 mysql -uroot -pzabbix -D "$DBNAME" -e "source /var/www/html/$REF/frontends/php/tests/selenium/data/data_test.sql"
fi

if [ ! -z $apijson ];then
zbx-util-color -H "api_json/data/data_test.sql"
docker exec zbx-box-mysql-5 mysql -uroot -pzabbix -D "$DBNAME" -e "source /var/www/html/$REF/frontends/php/tests/api_json/data/data_test.sql"
fi
}

mysql-8() {
zbx-util-color -H "DROP DATABASE IF EXISTS \"$DBNAME\""
docker exec zbx-box-mysql-8 mysql -uroot -pzabbix -e "DROP DATABASE IF EXISTS \`$DBNAME\`"
Expand Down Expand Up @@ -251,7 +278,9 @@ while [[ $# > 0 ]];do case $1 in
#- Quick open repl (use current API database, postgres).
-Aq | --api-db-query ) ex_dbs+=(api-db-query) ;;
#- Apply database to mariadb service.
-My | --mysql-8 ) ex_dbs+=(mysql-8) ;;
-My5| --mysql-5 ) ex_dbs+=(mysql-5) ;;
#- Apply database to mariadb service.
-My8| --mysql-8 ) ex_dbs+=(mysql-8) ;;
#- Apply database to mariadb service.
-M | --mariadb ) ex_dbs+=(mariadb) ;;
#- Quick open repl (use current database).
Expand All @@ -264,6 +293,8 @@ while [[ $# > 0 ]];do case $1 in
-Onq| --oracle-19c-query ) ex_dbs+=(oracle-19c-query) ;;
#- Quick open repl (use current database).
-Ooq| --oracle-11g-query ) ex_dbs+=(oracle-11g-query) ;;
#- Apply "integration" data set.
-I | --integration-data ) integration_data=1 ;;
#- Apply "api_json" data set.
-A | --api-json ) apijson=1 ;;
#- Prepare API json database (postgres)
Expand Down
3 changes: 3 additions & 0 deletions bin/zbx,git-activity
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
### Usage: $0 [?ago-days] [?days-till]
## Collects and prints recent commit messages acrross all workspaces.
Loading

0 comments on commit 73c035f

Please sign in to comment.