Koozali.org: home of the SME Server

[Solved] Template fragment question

Offline DanB35

  • ****
  • 764
  • +0/-0
    • http://www.familybrown.org
[Solved] Template fragment question
« on: February 24, 2016, 01:19:29 PM »
I'm having trouble getting a template fragment to work.  It's throwing errors which I expect relate to my not having escaped everything I need to escape.  Here's what I need to appear in the output:

Code: [Select]
if [ $1 = "deploy_cert" ] && [ $2 = "pbx.mydomain.tld" ]; then
  KEY=$3
  CERT=$4
  CHAIN=${5/fullchain.pem/chain.pem}
  scp $CERT root@pbx:/etc/pki/tls/certs/pbx.mydomain.tld.crt
  scp $KEY root@pbx:/etc/pki/tls/private/pbx.mydomain.tld.key
  scp $CHAIN root@pbx:/etc/pki/tls/certs/server-chain.crt
  ssh root@pbx "/sbin/service httpd reload"
  exit 0
fi

Here's what I have in the fragment:
Code: [Select]
{
    use strict;
    use warnings;
    use esmith::ConfigDB;

    my $configDB = esmith::ConfigDB->open_ro or die("can't open Config DB");

    my $letsencryptStatus = $configDB->get_prop( 'letsencrypt', 'status' )     || 'disabled';

    if ( $letsencryptStatus ne 'disabled' ) {

        $OUT .= "if [ \$1 = \"deploy_cert\" ] && [ \$2 = \"pbx.mydomain.tld\" ]; then\n"
        $OUT .= "  KEY=\$3\n"
        $OUT .= "  CERT=\$4\n"
        $OUT .= "  CHAIN=\${5/fullchain.pem/chain.pem}\n"
        $OUT .= "  scp \$CERT root@pbx:/etc/pki/tls/certs/pbx.mydomain.tld.crt\n"
        $OUT .= "  scp \$KEY root@pbx:/etc/pki/tls/private/pbx.mydomain.tld.key\n"
        $OUT .= "  scp \$CHAIN root@pbx:/etc/pki/tls/certs/server-chain.crt\n"
        $OUT .= "  ssh root@pbx \"/sbin/service httpd reload\"\n"
        $OUT .= "  exit 0\n"
        $OUT .= "fi\n"
    }
}

...but when I try to expand the template, I get these errors:
Code: [Select]
[root@e-smith hook-script.sh]# expand-template /usr/local/bin/hook-script.sh
WARNING in /etc/e-smith/templates-custom//usr/local/bin/hook-script.sh/05deploy_cert_PBX: Scalar found where operator expected at /etc/e-smith/templates-custom//usr/local/bin/hook-script.sh/05deploy_cert_PBX line 13, near ""if [ \$1 = \"deploy_cert\" ] && [ \$2 = \"pbx.familybrown.org\" ]; then\n"
        $OUT"
WARNING in /etc/e-smith/templates-custom//usr/local/bin/hook-script.sh/05deploy_cert_PBX: (Missing operator before
        $OUT?)
ERROR in /etc/e-smith/templates-custom//usr/local/bin/hook-script.sh/05deploy_cert_PBX: Program fragment delivered error <<syntax error at /etc/e-smith/templates-custom//usr/local/bin/hook-script.sh/05deploy_cert_PBX line 13, near ""if [ \$1 = \"deploy_cert\" ] && [ \$2 = \"pbx.familybrown.org\" ]; then\n"
        $OUT ">> at template line 1
ERROR: Template processing failed for //usr/local/bin/hook-script.sh: 2 fragments generated warnings, 1 fragment generated errors
 at /sbin/e-smith/expand-template line 45

I'm assuming that I missed escaping something, but it isn't obvious to me at first glance what it is.  Any suggestions?
« Last Edit: March 02, 2016, 02:39:27 PM by TerryF »
......

Offline Daniel B.

  • *
  • 1,699
  • +0/-0
    • Firewall Services, la sécurité des réseaux
Re: Template fragment question
« Reply #1 on: February 24, 2016, 01:36:39 PM »
You forgot the semi-colon at the end of your $OUT = XXX lines

But anyway, you'd better have something like

Code: [Select]
    $OUT .=<<"_EOF";
if [ \$1 = "deploy_cert" ] && [ \$2 = "pbx.mydomain.tld" ]; then
  KEY=\$3
  CERT=\$4
  CHAIN=\${5/fullchain.pem/chain.pem}
  scp $CERT root\@pbx:/etc/pki/tls/certs/pbx.mydomain.tld.crt
  scp $KEY root\@pbx:/etc/pki/tls/private/pbx.mydomain.tld.key
  scp $CHAIN root\@pbx:/etc/pki/tls/certs/server-chain.crt
  ssh root\@pbx "/sbin/service httpd reload"
  exit 0
fi
_EOF

(In this case, as your not using any perl variable, you could even use <<'_EOF' in which case there's no need to escape $ and @)
C'est la fin du monde !!! :lol:

Offline DanB35

  • ****
  • 764
  • +0/-0
    • http://www.familybrown.org
Re: Template fragment question
« Reply #2 on: February 24, 2016, 02:33:21 PM »
You forgot the semi-colon at the end of your $OUT = XXX lines
Well, I figured I'd feel dumb when I learned the answer, and I do--but now I know.  Thanks!

Quote
    $OUT .=<<"_EOF";
So much the better.  For the sake of posterity, I note that I also had to escape the $ in $CERT, etc.  It seems to be working now.  Thanks!
......

guest22

Re: Template fragment question
« Reply #3 on: March 02, 2016, 02:03:01 PM »
Please add [Solved] in front of the subject.

Offline DanB35

  • ****
  • 764
  • +0/-0
    • http://www.familybrown.org
Re: Template fragment question
« Reply #4 on: March 02, 2016, 02:24:17 PM »
It won't let me edit the first post to change the subject.
......

guest22

Re: Template fragment question
« Reply #5 on: March 02, 2016, 02:27:37 PM »
Bummer, thanks for trying.

Offline janet

  • ****
  • 4,812
  • +0/-0
Re: Template fragment question
« Reply #6 on: March 02, 2016, 08:35:56 PM »
It won't let me edit the first post to change the subject.

I think editing is allowed only for 7 days from the original post date.
Please search before asking, an answer may already exist.
The Search & other links to useful information are at top of Forum.

Offline Withave

  • 1
  • +0/-0
Re: Template fragment question
« Reply #7 on: August 17, 2016, 10:40:25 AM »
Well, I figured I'd feel dumb when I learned the answer, and I do--but now I know.  Thanks!
So much the better.  For the sake of posterity, I note that I also had to escape the $ in $CERT, etc.  It seems to be working now.  Thanks!

I also tried it and yeah it works! you just forgot the semi-colon at the end of your $OUT = XXX lines.. It's a good thing to know then!
Best website about crazy bulk http://supplementsmonkey.com/

Offline ReetP

  • *
  • 3,713
  • +5/-0
Re: [Solved] Template fragment question
« Reply #8 on: August 18, 2016, 09:19:11 PM »
Dan,

If you have a mod to the contrib then let me know and I can update it. I'm not on here so often so miss stuff. Just mail me.

B. Rgds
John
...
1. Read the Manual
2. Read the Wiki
3. Don't ask for support on Unsupported versions of software
4. I have a job, wife, and kids and do this in my spare time. If you want something fixed, please help.

Bugs are easier than you think: http://wiki.contribs.org/Bugzilla_Help

If you love SME and don't want to lose it, join in: http://wiki.contribs.org/Koozali_Foundation