Compile Exception In Script post upgrade

ruf-r
(muktesh) #1

After upgrade to the latest version of octopus deploy, our script started failing with the below error:
A using clause must precede all other elements defined in the namespace except extern alias declarations

        using System;
        using System.Net;
        using System.IO;
        using System.Collections.Generic;
        // Octopus.Parameters["ServerNames"] += Octopus.Parameters["Octopus.Machine.Hostname"];
        // var role = "Web-Server";//Octopus.Parameters["Octopus.Action[Enable Replication].TargetRoles"];
        //Console.WriteLine(role);
        var role = Octopus.Parameters["Role"];
        Console.WriteLine(role);
        string tempvar = "Octopus.Environment.MachinesInRole[{0}]";
        tempvar = string.Format(tempvar,role);
        string status = Octopus.Parameters["Status"];
        string servers  = Octopus.Parameters[tempvar];
        //string[] arr = Octopus.Parameters["ServerName"].ToList();
        var request = (HttpWebRequest)WebRequest.Create("URI"+servers+ "/"+status);
       // Uri baseAddress = new Uri("http://localhost:56019/");
        //client.BaseAddress = baseAddress;
        var response = request.GetResponse();
        var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
(Robert Wagner) #3

Hi Muktesh,

Thanks for getting in touch. I could not reproduce this problem on the latest version, the above script compiles fine.

Could you send me a raw log?

Also are you using a script module? If so, the problem could be in that module.

Lastly, which version did you upgrade from? If you are unsure, check the OctopusServerInstallationHistory table.

(muktesh) #4

Hi Robert,

Sorry for the delay.

No, we are not using any script module. We are calling a custom API using this script. It was working before upgrade and we have not made any change on this piece. Post upgrade we have started seeing this issue.

Please find the Raw Log below:

03:37:06 Verbose | Executing LB Offline SetA DC1 (type Run a Script) on Octopus Server
03:37:06 Verbose | Using Calamari 4.15.2
03:37:06 Verbose | Running this script in the Octopus Server security context (NT AUTHORITY\SYSTEM)
03:37:06 Verbose | Starting C:\Windows\system32\WindowsPowershell\v1.0\PowerShell.exe in working directory ‘C:\Octopus\Work\20190601093706-111111-6051’ using ‘OEM United States’ encoding running as ‘NT AUTHORITY\SYSTEM’ with the same environment variables as the launching process
03:37:07 Verbose | Octopus Deploy: Calamari version 4.15.2
03:37:07 Verbose | Environment Information:
03:37:07 Verbose | OperatingSystem: Microsoft Windows NT 10.0.14393.0
03:37:07 Verbose | OsBitVersion: x64
03:37:07 Verbose | Is64BitProcess: True
03:37:07 Verbose | CurrentUser: NT AUTHORITY\SYSTEM
03:37:07 Verbose | MachineName: XXXXXXXXX
03:37:07 Verbose | ProcessorCount: 2
03:37:07 Verbose | CurrentDirectory: C:\Octopus\Work\20190601093706-111111-6051
03:37:07 Verbose | TempDirectory: C:\Windows\TEMP
03:37:07 Verbose | HostProcessName: Calamari
03:37:07 Verbose | Performing variable substitution on ‘C:\Octopus\Work\20190601093706-111111-6051\Script.csx’
03:37:07 Verbose | Executing ‘C:\Octopus\Work\20190601093706-111111-6051\Script.csx’
03:37:09 Info | ERROR: Script compilation failed. [CompilationErrorException] C:\Octopus\Work\20190601093706-111111-6051\Script.csx(48,4): error CS1529: A using clause must precede all other elements defined in the namespace except extern alias declarations
03:37:09 Verbose | Process C:\Windows\system32\WindowsPowershell\v1.0\PowerShell.exe in C:\Octopus\Work\20190601093706-111111-6051 exited with code 1
03:37:09 Verbose | Updating manifest with output variables
03:37:09 Verbose | Updating manifest with action evaluated variables
03:37:09 Fatal | The remote script failed with exit code 1
03:37:09 Fatal | The action LB Offline SetA DC1 on the Octopus Server failed

Please let me know, if you need any more information on this.

Thanks,
Muktesh

(Robert Wagner) #5

I would like to have a look at the bootstrappers and the Script.csx file. To do this, could you get a copy of the working directory as described here. I won’t need the Variables.secret or the associated password. Send it in to support@octopus.com as this is a public forum.

Also a log file showing the evaluated variables may help.

(muktesh) #6

Hi Robert,

Thanks for your quick reply.

Can we get on a quick call? It will be very helpful for us. I’ll send an email to support@octopus.com as well with our official email IDs.

Please let me know the convenient time.

Thanks,
Muktesh

(muktesh) #7

Hi Robert,

Just to narrow it down in the script I using some clause, so for the compillation error I commented highlighted (yellow) part and it started working. Then I tried the console.writeline (highlighted in Green) to print the value of variables. I got the value for role, tempvar, status. But it gave the error Script execution failed. [KeyNotFoundException] The given key was not present in the dictionary for the variable server. Please find the attached file for the raw log.

While checking for the system variable Octopus.Environment.MachinesInRole[role], I came across one article (Get list of machines in a named role Octopus Deploy 2.4.7.85) where it has been mentioned that some bug has been fixed for this in previous versions.

Do we need to check for this system variable, as this variable is not getting the value to pass to the next line?

Please check on this and advise us on earliest.

//using System;
            using System.Net;
            using System.IO;
            using System.Collections.Generic;
            // Octopus.Parameters["ServerNames"] += Octopus.Parameters["Octopus.Machine.Hostname"];
            // var role = "Web-Server";//Octopus.Parameters["Octopus.Action[Enable Replication].TargetRoles"];
            //Console.WriteLine(role);
            var role = Octopus.Parameters["Role"];
            Console.WriteLine(role);
            string tempvar = "Octopus.Environment.MachinesInRole[{0}]";
                                           Console.WriteLine(tempvar);
            tempvar = string.Format(tempvar,role);
                                           Console.WriteLine(tempvar);
            string status = Octopus.Parameters["Status"];
                                           Console.WriteLine(status);
            string servers  = Octopus.Parameters[tempvar];
                                           Console.WriteLine(servers);
            //string[] arr = Octopus.Parameters["ServerName"].ToList();
            var request = (HttpWebRequest)WebRequest.Create("URI"+servers+ "/"+status);
            //client.BaseAddress = baseAddress;
            var response = request.GetResponse();
            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

Please check on this and let me know, if i need to provide more information on this.

Thanks,
Muktesh

1 Like